0
Python modulo negative numbers
9%2=1 9%3=0 3%9=3 how about -9%3 -3%9 9%-3 3%-9 -9%-3 -3%-9 -9%-9 -9%9 9%-9 is there any formula to calculate this?
5 Answers
+ 1
thank you for the explanation
+ 1
One more thing, why 9%-4 is -3 but not 1
+ 1
There are two conventions, depending on whether you allow the remainder to be negative:
ā17 = ā5 * 3 + ā2
or not:
ā17 = ā6 * 3 + 1
Python's modulo operator always return a remainder that has the same sign as the denominator:
-9%3 = 0 -> no remainder
-3%9 = 6 -> -3 = -1 * 9 + 6
9%-3 = 0 -> no remainder
3%-9 = -6 -> 3 = -1 * -9 + -6
-9%-3 = 0 -> no remainder
-3%-9 = -3 -> -3 = 0 * -9 + -3
-9%-9 = 0 -> no remainder
-9%9 = 0 -> no remainder
9%-9 = 0 -> no remainder
+ 1
ok thank you guys
+ 1
This was really complicatedš, thank you for such a simple explanation