Why is this happening?
It's defined that way in C. %
is the remainder operator. Per 6.5.5 Multiplicative Operators, paragraph 6 of the C Standard:
When integers are divided, the result of the /
operator is the
algebraic quotient with any fractional part discarded. If the
quotient a/b
is representable, the expression (a/b)*b + a%b
shall equal a
; otherwise, the behavior of both a/b
and
a%b
is undefined.
It's also defined that way in Java. Per 15.17.3. Remainder Operator % of the Java 8 specification:
The binary % operator is said to yield the remainder of its operands
from an implied division; the left-hand operand is the dividend and
the right-hand operand is the divisor.
In C and C++, the remainder operator accepts only integral operands,
but in the Java programming language, it also accepts floating-point
operands.
The remainder operation for operands that are integers after binary
numeric promotion (§5.6.2) produces a result value such that
(a/b)*b+(a%b) is equal to a.
This identity holds even in the special case that the dividend is the
negative integer of largest possible magnitude for its type and the
divisor is -1 (the remainder is 0).
It follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive
only if the dividend is positive. Moreover, the magnitude of the
result is always less than the magnitude of the divisor.
So the remainder - the result of the %
operator - must have the same sign as the dividend - the first value, before the %
operator.