This is to preserve the identity that (x / y) * y + (x % y) == x
for all integers x
and y != 0
; this is what it means for x % y
to be the remainder when x
is divided by y
.
In Java, integer division rounds towards zero, but this means the remainder operator %
can give negative results, and this is inconvenient for most purposes. For example, the expression arr[i % arr.length]
doesn't guarantee a valid array index when i
is negative.
In Python, if y
is positive then x % y
always gives a non-negative result (in the range from 0
to y - 1
inclusive), so to preserve the "remainder" property, integer division has to always round down, not necessarily towards zero.