Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add % operator logic to PyDough #239

Open
vineetg3 opened this issue Jan 31, 2025 · 0 comments
Open

Add % operator logic to PyDough #239

vineetg3 opened this issue Jan 31, 2025 · 0 comments
Labels
documentation Improvements or additions to documentation effort - low quick & simple issue enhancement New feature or request user feature Adding a new user-facing feature/functionality

Comments

@vineetg3
Copy link
Contributor

vineetg3 commented Jan 31, 2025

The modulo (%) operator is used to calculate the remainder of a division operation between two numbers. For example:
A % B = R (where R is the remainder when A is divided by B). This issue would resolve just the support of % operator.

The general formula for % is:
remainder = a - (b * (quotient)).
The quotient depends on how division (a / b) is performed in the specific language (e.g., truncating or floor-based division)
When dealing with negative numbers:
In SQL, the modulo behavior is C++ like, where the result's sign matches the dividend, and the decimal part is truncated.
In Python, the result's sign matches the divisor, and the decimal part is rounded to negative infinity. Having the mod behave like Python simplifies mathematical operations, hence PyDough's implementation would be the same.

Here's a small table for clarification:

Comparison Between SQL, Python, and C++

Feature/Behavior SQL (MOD() or %) Python % C++ %
Sign of Result Matches the dividend Matches the divisor Matches the dividend
Division Behavior Truncates toward 0 Floor division Truncates toward 0
Formula a - (a / b) * b a - b * floor(a / b) a - (a / b) * b
Examples (-10 % 3) -1 2 -1
Examples (10 % -3) 1 -2 1
Examples (-10 % -3) -1 -2 -1
Examples (10 % 3) 1 1 1

This feature needs to be documented in the function list documentation

@vineetg3 vineetg3 added documentation Improvements or additions to documentation effort - low quick & simple issue enhancement New feature or request user feature Adding a new user-facing feature/functionality labels Jan 31, 2025
@vineetg3 vineetg3 changed the title Add % operator to PyDough Add % operator logic to PyDough Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation effort - low quick & simple issue enhancement New feature or request user feature Adding a new user-facing feature/functionality
Projects
None yet
Development

No branches or pull requests

1 participant