Modulo Calculator
The remainder after division — with both conventions, because programming languages disagree about negative numbers.
a mod b is what is left over after dividing a by b.
How the modulo calculator works
a mod b is what is left over after dividing a by b. For positive numbers everyone agrees: 17 mod 5 is 2.
Negatives are where languages split. JavaScript, C and Java truncate the quotient toward zero — trunc(−17/5) is −3, leaving −17 − (−15) = −2. Python, Ruby and mathematicians floor it instead — floor(−17/5) is −4, leaving −17 − (−20) = 3, a result that always carries the sign of the divisor. Both are shown here, because picking the wrong one silently breaks anything that wraps around: clock arithmetic, array indices, hash buckets.
Formula: truncated: a − b·trunc(a/b); floored: a − b·floor(a/b)
Worked examples
| Inputs | a mod b (floored) | Note |
|---|---|---|
| 17 mod 5 | 2 | 2 |
| −17 mod 5 — the two conventions differ | 3 | 3 floored, −2 truncated |
| 100 mod 7 | 2 | 2 |
FAQFrequently asked questions
What is the modulo operator?
It gives the remainder after division. 17 mod 5 is 2, because 5 goes into 17 three times with 2 left over.
What is −17 mod 5?
It depends on the convention: 3 in Python and in mathematics, −2 in JavaScript, C and Java. Both are shown above.
Why do languages disagree on negative numbers?
Because they round the quotient differently — toward zero (truncated) or downward (floored). Both satisfy a = bq + r; they just choose different q.
Which one should I use?
The floored version for anything cyclic — clock faces, angles, wrapping indices — because it never returns a negative.
Can the divisor be zero?
No. Division by zero is undefined, so there is no remainder to give.
Where these figures come from
- NIST Digital Library of Mathematical Functions — reference definitions for elementary and special functions
- Wolfram MathWorld — definitions and formulas for every topic on this page
- NIST/SEMATECH e-Handbook of Statistical Methods — the statistical formulas (mean, variance, z, confidence intervals, sample size)
- Common Core State Standards — Mathematics — the terms and methods taught in US schools
Last checked: September 2026. Formulas are fixed by mathematics and do not change with tax years or regulations.