How this calculator works
Modulo gives the remainder left over after dividing one number by another. For positive numbers this is unambiguous — 7 mod 3 is 1 either way you compute it — but for negative numbers, two different conventions produce different answers, and mixing them up is a common source of off-by-one bugs.
This calculator shows both results side by side: the floored modulo used in mathematics and in languages like Python, and the truncated remainder produced by JavaScript's own % operator. Knowing which one your code actually uses matters whenever negative numbers are involved.
The two conventions
Floored modulo: a mod n = a − n × floor(a / n) — result always takes the sign of nJavaScript remainder: a % n = a − n × trunc(a / n) — result always takes the sign of aThe two conventions agree whenever a and n have the same sign, or when a is divisible by n. They disagree whenever a and n have opposite signs and there is a nonzero remainder — exactly the case most likely to surprise you in real code.
Worked example: -7 mod 3
- Floored: floor(-7 / 3) = floor(-2.333...) = -3, so -7 mod 3 = -7 − 3×(-3) = -7 + 9 = 2.
- JavaScript: -7 % 3 truncates -7/3 toward zero to -2, so -7 % 3 = -7 − 3×(-2) = -7 + 6 = -1.
- Both are 'correct' remainders under their own rule, but only the floored result (2) falls in the conventional range [0, 3) that most modulo use cases expect.
- Check: 3 × (-3) + 2 = -7, confirming the floored division identity a = n×q + r.
Frequently asked questions
Why does JavaScript's % give a negative result for -7 % 3?
JavaScript's % operator implements truncated division, which rounds the quotient toward zero rather than toward negative infinity. Truncating -7/3 (≈ −2.33) toward zero gives -2, leaving a remainder of -1, whose sign matches the dividend (-7) rather than the divisor.
Which convention should I use for clock or array-index wraparound?
Floored modulo, almost always. If you compute a negative hour or index and want it to wrap around correctly (for example, -5 mod 12 should land on 7, not -5), floored modulo is what wraps values back into the expected 0-to-(n−1) range.
How do I get floored modulo in JavaScript?
Since JavaScript only provides truncated %, the floored result is commonly computed as ((a % n) + n) % n, or equivalently a − n × Math.floor(a / n), which is the formula this calculator uses.
Does the divisor's sign matter?
Yes. Floored modulo always takes the sign of the divisor n (nonnegative when n is positive, nonpositive when n is negative), while JavaScript's % always takes the sign of the dividend a, regardless of the divisor's sign.
What happens if the divisor is 0?
Division and modulo by zero are undefined, so this calculator does not return a result when n = 0.