-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
mod seemingly dispatches to the rust remainder operator rather than the modulo operator #10570
Comments
I much prefer Python's definition of However this also means we must update integer division to be consistent with it. The identity |
There has been a discussion about this before where we decided we keep true to our backend. I cannot find that issue atm. |
I think this is it here |
@orlp I don't think it's a question of preference. Modulo division is well established so if a programming language or library says it's doing modulo division then the result should be the same or else it's doing the wrong thing. @ritchie46 In the other issue, the confusion was strictly about the % operator. Rust defines the % operator as remainder division whereas python defines % as modulo division. That's just a question of convention as there's nothing in math that says what % should mean. That's an open point of confusion but I don't think there's an inherently right or wrong decision. By contrast rust has mod too. If I'm not mistaken, the current situation is that polars's mod uses rust's % instead of rust's mod. |
@deanm0000 Euclidean division/modulo in Rust is still wrong for negative divisors.
Unfortunately, that's not the case. There are many variants all referred to as 'modulo': https://en.wikipedia.org/wiki/Modulo Again, I would strongly prefer Python's definition because I think it's the most mathematically elegant. But it's not (sadly) universal. |
If I am correct, I don't think floor_div (floordiv) and rem (mod) in polars currently satisfy this. >>> x = -1
>>> y = 3
>>> x == (x % y) + y * (x // y)
True
>>> import polars as pl
>>> x_pl = pl.lit(x)
>>> y_pl = pl.lit(y)
>>> pl.select(x_pl == (x_pl % y_pl) + y_pl * (x_pl // y_pl))
shape: (1, 1)
┌─────────┐
│ literal │
│ --- │
│ bool │
╞═════════╡
│ false │
└─────────┘
>>> pl.select(x == (x % y) + y * (x // y))
shape: (1, 1)
┌─────────┐
│ literal │
│ --- │
│ bool │
╞═════════╡
│ true │
└─────────┘ |
Current behaviour is very unexpected for a python math lib. It felt like debugging if something as simple as addition is working as expected. |
This does not seem to have been fixed yet. |
Checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of Polars.
Reproducible example
This originated at this SO question
Issue description
I found this which confirms BallPointBen's assertion that there's a mismatch in operators.
Expected behavior
mod should match the python % operator and np.mod
Installed versions
The text was updated successfully, but these errors were encountered: