Why does -103/100 == -2
but 103/100 == 1
in Python? I can't seem to understand why.
Integer division always rounds down (towards negative infinity).
Plain or long integer division yields an integer of the same type; the result is that of mathematical division with the floor1 function applied to the result.
http://docs.python.org/2/reference/expressions.html#binary-arithmetic-operations
This allows for the integer division and modulo (remainder, %
) operators to connect nicely through the identity x == (x/y)*y + (x%y)
.
1 floor(x) is the largest integer not greater than x.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With