Re:Why Python's Integer Division Floors (2010)
by shikon7 on Wednesday February 28, 2024 @06:51PM
(#39545012)
But 2 mod 3 should still give the same as -1 mod 3. If the representative for the same equivalence class is allowed to be different depending on the input, you could just as well use the number itself.
[ Parent
| on HN ]
Re:Why Python's Integer Division Floors (2010)
by marko_oktabyr on Wednesday February 28, 2024 @07:40PM
(#39545317)
At the risk of being snarky, I should certainly hope the output depends on the input.
On a more serious note, there are quite reasonable properties that are satisfied by keeping the sign of the first argument as the chosen representative. In particular, for any two nonzero integers a and b, we have that:
a = (a / b) * b + (a mod b)
Where division should be interpreted as integer division that rounds towards zero. If a=-1 and b=3, then (a/b) would be zero which would require (a mod b) to be -1 if we want the above to hold. Also note that other rounding choices (e.g., rounding towards negative infinity) could impose (a mod b) = 2.
So choosing a particular representative comes down to choosing what properties you want your function to have in relation to other arithmetic.
Re:Why Python's Integer Division Floors (2010)
by oasisaimlessly on Thursday February 29, 2024 @03:06AM
(#39547342)
There is no "particular representative"; every equivalence class (except 0) has two possible representatives.
... and that's exactly why the C modulo operator sucks.