Why do so many computer programming language implementations have trouble with the remainders of negative integers?

I believe that the primary justification for this is that division in these languages generally rounds towards 0 instead of towards -infinity when doing integer division (questionably) and they want to preserve the identity that (a // b) * b + (a mod b) = a (rightly). From a mathematical standpoint I completely agree that a mod b should always be in [0, b) and do question the original justification for this decision, however for compatibility it is unreasonable to try and change the standard now. Note that there are other languages beyond the ones you have mentioned, such as Python which do behave in this way.


Hint: The paper The Euclidean Definition of the Functions $\mathrm{div}$ and $\mathrm{mod}$ by R.T. Boute provides an in-depth discussion of different definitions in mathematics and programming languages.

I'm a great fan of D.E. Knuth and since his F-definition (flooring the quotient and rounding toward negative infinity) is addressed in the paper as one of two preferred approaches, I also recommend reading section 3.4 '$\mathrm{MOD}$' - THE BINARY OPERATION in Concrete Mathematics.


Let there be given integers $m \lt 0$ and $b \gt 0$.

Proposition: There exist unique integers $q$ and $r$ satisfying the following conditions:

$\quad q \le 0$

$\quad0 \le -r \lt b\;$ (so $r$ is a nonpositive integer)

$\quad m = qb + r$

The proof is left as an exercise for the interested reader.

If you are working with mixed numbers/improper fractions, this might float your boat; see How to Convert a Negative Mixed Number Into an Improper Fraction : Fractions 101. So $-7/4 = -1 \frac{3}{4}$, and some might insist that $-7 \equiv -3 \pmod 4$ make sense.