Add % operator logic to PyDough #239
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
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++
MOD()
or%
)%
%
a - (a / b) * b
a - b * floor(a / b)
a - (a / b) * b
-10 % 3
)-1
2
-1
10 % -3
)1
-2
1
-10 % -3
)-1
-2
-1
10 % 3
)1
1
1
This feature needs to be documented in the function list documentation
The text was updated successfully, but these errors were encountered: