This is just a convention chosen by language. There is more than one choice that is more mathematically correct than another.
Hints for Python selection reasons can be found in the source: http://hg.python.org/cpython/file/2.7/Objects/intobject.c#l567
The Python version is the most common in number theory. The Java version implements the same behavior as C. The Python path is convenient because the sign of the result is always the same sign as the divisor. This means that x % n
always gives a value of 0 <= result < n
when n is positive.
I assume that the reason a lower level language such as C uses a different convention is because it is "closer to metal" returns the same result as n signed division into machine language (see IDIV , eg). If C accepted the agreement on number theory, it would have to compile additional instructions to compare the sign of the result with the sign of the divisor and if they did not match, then go to the additional code to add or subtract the divisor from the result (IOW, C accepts the agreement, corresponding to minimal effort).
source share