In C89 / 90, either the result was resolved. The results obtained by dividing and the remainder had to βmatchβ together so that (a/b)*b + a%b == a .
Starting with C99, division by zero requires integer division with a negative input. A link between the separation and residue results is still required. This means that (in fact) the result of the remainder operation always has the same sign as the left operand, so 25% -9 should give 7, not -2. A.
For what it costs, C ++ followed roughly the same path, just a few years behind C. C ++ 98/03 has the same rules as C89 / 90, so the remainder can be negative or positive for your inputs (but still must be combined with the result of the division). Starting with C ++ 11, it requires the same behavior as C99, so 25% - 9 == 7.
Some other languages ββ(e.g. Python) require that the result from the remainder has the same sign as the right operand.
source share