Modulo Calculator
The remainder of a division under all three conventions programming languages use, which disagree for negative numbers, and modular powers done exactly.
Result
Remainder, truncated: C, Java, JavaScript: 2
For whole numbers. The three conventions agree whenever both numbers are positive.
- Remainder, floored: Python, Ruby
- 2
- Remainder, Euclidean: never negative
- 2
- Quotient, truncated
- 3
https://calcgallery.com/algebra-numbers/modulo-calculator/
Results follow the mathematical operation and values shown. Check that you selected the intended operation, convention and dataset type before relying on the result. Read the full disclaimer
How to use the Modulo Calculator
- Enter the whole number to divide, a, and the number to divide by, b.
- Read the remainder under each of the three conventions, and the quotient.
- To raise a to a power e and take the remainder, choose the modular power and enter e.
The three conventions
truncated r = a − b × trunc(a ÷ b) takes the sign of afloored r = a − b × floor(a ÷ b) takes the sign of bEuclidean r = a mod |b|, adjusted to 0 ≤ r < |b|
When a and b are both positive, all three give the same answer. They differ only when one or both is negative, which is exactly when a program ported from one language to another starts to misbehave.
Modular exponentiation
Squaring and reducing at each step keeps every intermediate number small, so a power with an exponent of a million is instant and exact. It is the operation at the heart of public-key cryptography.
With your numbers
- 17 = 5 × 3 + 2
Quick reference
For whole numbers. The three conventions agree whenever both numbers are positive.
| Example | Result |
|---|---|
| 17 mod 5 | Remainder, truncated: C, Java, JavaScript: 2 |
| −7 mod 3 | Remainder, truncated: C, Java, JavaScript: -1 |
| 7 mod −3 | Remainder, truncated: C, Java, JavaScript: 1 |
| −7 mod −3 | Remainder, truncated: C, Java, JavaScript: -1 |
| 10 mod 5 | Remainder, truncated: C, Java, JavaScript: 0 |
| 2 to the 100, mod 7 | a to the e, mod b: 2 |
Common questions about the Modulo Calculator
- Why does −7 mod 3 give different answers?
- Because languages disagree on which way to round the quotient. JavaScript, C and Java truncate toward zero, so −7 ÷ 3 is −2 remainder −1. Python and Ruby round down, so it is −3 remainder 2. Mathematicians usually want a remainder that is never negative, the Euclidean one, which is also 2. All three are correct for their convention; the page shows each.
- Which should I use?
- Whichever your language or textbook uses. For clock arithmetic, array indexing that wraps around, or anything where a negative remainder would be a bug, the Euclidean remainder from 0 up to |b| − 1 is usually what you want.
- How is a huge power taken mod a number?
- By repeated squaring, taking the remainder at every step, so the numbers never grow larger than the modulus squared. 2 to the power 100, mod 7, is 2. Computing 2¹⁰⁰ in full first works here, but for most bases and large exponents the full power is far too big to hold exactly.
- What about dividing by zero?
- There is no remainder: division by zero is not defined, and the page says so.