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?

17th Mar 2020, 12:05 PM
Свен
Свен - avatar
8 Réponses
+ 1
thank you for the explanation
17th Mar 2020, 12:22 PM
Свен
Свен - avatar
+ 1
One more thing, why 9%-4 is -3 but not 1
17th Mar 2020, 12:34 PM
Свен
Свен - avatar
+ 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
17th Mar 2020, 12:42 PM
andriy kan
andriy kan - avatar
+ 1
ok thank you guys
17th Mar 2020, 12:45 PM
Свен
Свен - avatar
+ 1
This was really complicated🙈, thank you for such a simple explanation
17th Mar 2020, 1:03 PM
Свен
Свен - avatar