The operation n/10 results, over the integers, in runs of exactly 10 identical results, followed by the next integer. If -7/10 had the terrible result of 0, then the above sequence would have 20 zeros in a row instead of 10, making that single number an anomaly in what is otherwise a perfectly symmetrical sequence.
In general, when an integer n is divided by an integer d to obtain an integer quotient q and an integer remainder r, the numbers are related as follows: n = dq + r. Python requires the integer remainder of an integer division by a positive integer to be positive, which agrees with the mathematical definition of modulus in number theory. When -7 is divided by 10, the only way to achieve this is for the remainder to be 3 and the quotient to be -1 because -7 = (10)(-1) + 3.
Comment
In Python integer division why does -7/10 == -1 and not 0?
Replies
That's a really good question. I honestly don't know the answer.
If you do -7/float(10) what's the answer?
That's very different. That's -7/10.0 which is something else.
For beauty and symmetry. Observe:
>>> [n/10 for n in range(-30, 30)]
[-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
The operation n/10 results, over the integers, in runs of exactly 10 identical results, followed by the next integer. If -7/10 had the terrible result of 0, then the above sequence would have 20 zeros in a row instead of 10, making that single number an anomaly in what is otherwise a perfectly symmetrical sequence.
That's nice but a strangely beautiful and weird explanation.
In general, when an integer n is divided by an integer d to obtain an integer quotient q and an integer remainder r, the numbers are related as follows: n = dq + r. Python requires the integer remainder of an integer division by a positive integer to be positive, which agrees with the mathematical definition of modulus in number theory. When -7 is divided by 10, the only way to achieve this is for the remainder to be 3 and the quotient to be -1 because -7 = (10)(-1) + 3.
Thank you!