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
5 Answers
+ 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