There are three different obvious ways to define integer division over the negative numbers, and three corresponding ways to define remainder:
- floored division, where the quotient is floored toward negative infinity and the remainder has the same sign as the divisor.
- truncated division, where the quotient is truncated toward zero and the remainder as the same sign as the dividend.
- Euclidean division, where the quotient is truncated in whichever direction leaves the remainder positive.
All three of these preserve the fundamental law of integer division:
dividend = divisor * quotient + remainder
So, none of the three are "right" or "wrong".*
Of course that doesn't stop people from having holy wars. Knuth argued for floored division on the basis that it was "mathematically correct". Wirth argued for truncated division because it's "less surprising". Raymond Boute argued that integer division is defined in terms of the Euclidean algorithm. I won't try to settle a decades-old holy war by arguing that all three of them, including two of the most important people in the field, are wrong…
Some languages solve this problem by having two different functions.**
Let's just say that Python picked up Knuth's definition, so its modulus operator has the sign of the divisor.
* Of course picking non-matching definitions of quotient and remainder is another story. Or, worse, specifying one and leaving the other implementation-defined, as C did before C99.
** This is especially fun because they're not always consistent about which one is which. At least when they're called rem
and mod
, rem
is the one that goes with div
, while mod
is whichever of floored or Euclidean doesn't go with div
, when when they're called rem
and remainder
or mod
and modulo
…