When a-b*i
becomes negative, why is -8 % 5
2
, rather than 3
or -3
, when using Python 2.7.
a = 12
b = 5
for i in range(10):
print a-b*i, (a-b*i) % b
12 2
7 2
2 2
-3 2
-8 2
-13 2
-18 2
-23 2
-28 2
-33 2
When a-b*i
becomes negative, why is -8 % 5
2
, rather than 3
or -3
, when using Python 2.7.
a = 12
b = 5
for i in range(10):
print a-b*i, (a-b*i) % b
12 2
7 2
2 2
-3 2
-8 2
-13 2
-18 2
-23 2
-28 2
-33 2
You can check it yourself using google calculator:
https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=-8+%25+5
when you do Euclidian divison of a/n, you get a reminder. The whole deal should satisfy an equation
a = qn + r
where q is a quotient, that belongs to Z ( integers from -inf to +inf)
So in your case
-8 = 5 * (-2) + 2
thus
r = 2 = (-8) % 5