As most of us know, or should know, $-7 \equiv 1 \pmod 4$.
But if you use Java's modulus operator %, you get -3 for the answer, not 1. That's technically correct, but it can cause problems if you're not aware of this as you write a program. C# and Javascript are the same way.
Worse, there are varieties of BASIC which will give you the remainder of the absolute value, e.g., 3 for the example of $-7$; that's clearly wrong.
It looks like only Maple and Mathematica (and by extension, Wolfram Alpha) know better.
Why is this?
(x div y)*y + x mod y = xwith $ 0 \le x \bmod y < y$ if $x \ge 0, y>0$. The situation really gets wierd if $y < 0!$ – gammatester Aug 22 '17 at 21:37%modulus operator returns a remainder with the same sign as the divisor, and it provides a//floor division operator (i.e., it rounds towards negative infinity). If you like, you can get both quotient & remainder in a single built-in function call:q, r = divmod(n, d); the division & modulus operators / function preserve the identityn = q * d + r. – PM 2Ring Aug 22 '17 at 21:40%for remainder is, to the best of my knowledge, inherited from the C language (or rather its predecessor, B) in the early 1970s. Since C is a systems-level programming language, it made a lot of sense for the software specification to follow the hardware. – Erick Wong Aug 22 '17 at 22:11(-7)%4is $1$ in Lua. Tryprint((-7)%4)in the Lua online demo. – lhf Aug 23 '17 at 00:36