Minus Seventeen Modulo Five Has Two Different Answers
Published 10/16/2026 · 3 min read · Developer tools
Daniel Okonkwo — Front-end developer and tech writer at OneKitly
Web performance · File formats
Checked against 2 sources
The remainder of 17 divided by 5 is 2 and nobody disagrees. Give it a negative dividend and the answer splits, because the languages disagree about the quotient rather than the remainder: −17 ÷ 5 is either −3 with a remainder of −2, or −4 with a remainder of 3, and both satisfy the identity that the quotient times the divisor plus the remainder equals the dividend. Languages that truncate the quotient toward zero — JavaScript, C, Java, Go — return −2 and give the remainder the sign of the dividend. Languages that floor it — Python, Ruby, and the MOD function in spreadsheets — return 3 and give the remainder the sign of the divisor. This calculator uses the first convention.
17 mod 5 is 2 everywhere. −17 mod 5 is −2 in JavaScript, C and Java, and 3 in Python, Ruby and a spreadsheet. Both are right, and the difference is a real bug.
The bug always looks the same
Wrapping an index around an array is the canonical case. Moving forward with (i + 1) % n behaves perfectly, so nobody thinks twice about writing (i − 1) % n for the other direction — and in a truncating language that returns −1 when i is 0, which indexes nothing and usually surfaces three screens later as an empty carousel slide or a crash in a reducer. The fix is the standard incantation ((i − 1) % n + n) % n, which forces the result positive in any language: for n = 5 and i = 0 it gives 4, the last element, which is what wrapping backwards was supposed to mean. Write it once in a helper and stop thinking about it.
Modulo is the arithmetic of anything that cycles
Clocks, days of the week, colour wheels, ring buffers and hash tables all run on it, and so does the check digit on the back of a bank card. Wherever a value has to come back round to the start, the remainder is what does the coming round — and reading a divisor as "how many hours on this clock face" makes most modulo code obvious at a glance. It also explains why the sign question matters so much in practice: a clock that reads −2 o'clock is not wrong, it just has not been told that this particular clock only shows numbers from 0 to 4.
| Expression | Truncated (JS, C, Java) | Floored (Python, Ruby, MOD) |
|---|---|---|
| 17 mod 5 | 2 | 2 |
| −17 mod 5 | −2 | 3 |
| 17 mod −5 | 2 | −3 |
Worked with our own calculator
Modulo calculator
Given
- Dividend
- 9
- Divisor
- 3
Result
- Remainder (a mod b)
- 0
- Quotient
- 3
These figures are produced by the calculator below, not typed in by hand — they are recomputed whenever the tool changes.
Run it on your own figures →Frequently asked questions
- What does it do with decimals?
- It still works — the remainder of 17.5 by 5 is 2.5 — but binary floating point makes the result untrustworthy at the edges. Values that look exact in decimal are not exact in binary, so an expression that ought to land on zero can land on a vanishingly small positive number or on the divisor itself, and a test for "is the remainder zero" then fails on numbers that plainly divide. Where the check matters, scale to integers first — work in cents rather than in currency, in milliseconds rather than in seconds — and keep the modulo away from the floating point entirely.
- Which convention should I prefer?
- The floored one, whenever you get to choose. A remainder that always carries the sign of the divisor stays inside the range you actually want when the divisor is positive, which is nearly every practical use, and it makes the wrap-around helper unnecessary. The truncated convention exists mostly because early hardware divided that way and C followed the hardware; it survives by inertia and by compatibility rather than by merit. Since you rarely get to choose the language, the useful habit is simply to check which one you are in before writing modulo on anything that could go negative.
Articles you may find interesting
All guides →Related tools
Sources
Spotted a mistake in this article?