This calculator computes a mod n and shows the remainder under both common conventions plus the integer quotient. It is for programmers and students who need to know exactly what a remainder will be, especially with negative numbers where languages disagree.
How it works
Two definitions are computed from the dividend (a) and divisor (n):
- Truncated (JavaScript
%, C, Java): remainder = a − n × trunc(a ÷ n). Its sign follows the dividend a. - Floored (Python
%, mathematical “mod n”): remainder = ((a % n) + n) % n, always in the range 0 to |n|. Its sign follows the divisor n.
The quotient shown is the truncated integer quotient, trunc(a ÷ n). The two remainders are identical whenever both numbers share a sign; they differ only when exactly one is negative. Division by zero is reported as undefined.
Example
With a = -17 and n = 5:
- Truncated: -17 − 5 × (-3) = -17 + 15 = -2
- Floored: ((-17 % 5) + 5) % 5 = (-2 + 5) % 5 = 3
- Quotient (truncated): -3
| a mod n | Truncated | Floored |
|---|---|---|
| 17 mod 5 | 2 | 2 |
| -17 mod 5 | -2 | 3 |
| 17 mod -5 | 2 | -3 |
All arithmetic happens locally in your browser, so the numbers you enter never leave your device.
Where the difference actually matters in code
For most everyday uses of the % operator — checking if a number is even, finding array indices, cycling through values — both conventions give the same result because both operands are positive. The difference only shows up when one or both numbers is negative, and in those cases it can cause subtle bugs that are hard to trace.
Circular / wrapping arithmetic
A common use case is wrapping an index around a fixed-size range — for example, cycling through days of the week or implementing a ring buffer. If the index can go negative (from subtraction or a user-provided offset), the two conventions diverge:
// JavaScript — truncated % can return negative
function prevDay(today) {
return (today - 1) % 7; // if today=0, returns -1 (wrong!)
}
// Correct in JS using floored-style workaround
function prevDay(today) {
return ((today - 1) % 7 + 7) % 7; // always 0–6
}
Python’s % handles this naturally because it uses the floored convention and always returns a non-negative result when the divisor is positive.
Hash table indexing
When mapping a hash code (which may be negative in Java and C) to a bucket index, using % directly can produce a negative bucket number and an ArrayIndexOutOfBoundsException. The standard fix is ((hash % n) + n) % n or, in Java, Math.floorMod(hash, n).
Cryptographic and number-theoretic code
Mathematics defines mod n as the floored convention, producing a result in [0, n). Cryptographic algorithms expressed in mathematical notation therefore expect floored behavior. When translating pseudocode from a paper into JavaScript or C, the truncated % will silently produce wrong results for negative inputs. Always use a floored-mod helper function in these contexts.
Language reference
| Language | Operator | Convention | Negative-dividend result |
|---|---|---|---|
| Python | % | Floored | Always non-negative (if n > 0) |
| JavaScript | % | Truncated | Same sign as dividend |
| Java | % | Truncated | Same sign as dividend |
| C / C++ | % (C99+) | Truncated | Same sign as dividend |
| Ruby | % | Floored | Always non-negative (if n > 0) |
| Go | % | Truncated | Same sign as dividend |