Here is the proof:
-8 == -2 * 3 - 2
that means -8%3
should be equal to -2
.
but python returns 1
and it's driving me crazy
In python, the sign matches the denominator.
>>> -8 % 3
1
>>> -8 % -3
-2
For an explanation of why it was implemented this way, read the blog post by Guido.
integer math is funny:
>>> -8//3 # (-8/3 in python2 does the same thing)
-3
>>> 8//3 # (8/3 in python2 does the same thing)
2
>>>
Rounding is done down, not towards zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With