Compute the remainder of a division, with true mathematical modulo for negatives.
Modulo gives the remainder after division. For non-negative dividends all three conventions agree. They diverge only when the dividend or divisor is negative, because the conventions differ in which direction they round the quotient: toward zero (truncated), toward minus infinity (floored), or to the nearest integer that keeps the remainder non-negative (Euclidean). In everyday clockwork arithmetic (hours of the day, angles, hash table indices) the Euclidean version is what you want.
Euclidean modulo
a mod b = a − b × floor(a/b)
Truncated remainder (JavaScript %)
a % b = a − b × trunc(a/b)
−7 % 3 = −1 in JavaScript because −7 = 3 × (−2) + (−1) and the quotient rounds toward zero. −7 mod 3 = 2 in the Euclidean sense because −7 = 3 × (−3) + 2 and the quotient rounds toward negative infinity. The identity 'dividend = divisor × quotient + remainder' holds for both — they just choose different quotients.
Use the Euclidean version (the formula ((a % b) + b) % b) for anything that must be non-negative: day-of-week arithmetic, wrap-around array indices, and hue angles. The raw % operator is fine when both operands are guaranteed to be positive.
For positive integers, yes. For negatives they can differ depending on which quotient convention your long division uses.