Modulo Calculator
a mod n for any two numbers: the remainder when both are positive (17 mod 5 = 2), and the sign of the divisor for negatives (-7 mod 3 = 2). How it works
Use the Modulo Calculator
About the Modulo Calculator
a mod n = a − n × ⌊a ÷ n⌋, so the result has the sign of the divisor: 10 mod 3 = 1, −7 mod 3 = 2 and 10 mod −3 = −2. For two positive numbers that is the ordinary remainder; for a negative dividend it is not the remainder long division gives (−7 ÷ 3 is −2 remainder −1). Decimals are worked out exactly, so 0.3 mod 0.1 = 0, and a number such as 9007199254740993 keeps every digit: 9007199254740993 mod 10 = 3. The quotient is shown too (−7 = −3 × 3 + 2), and the operation menu adds (a + b) mod n, (a × b) mod n and powers: 4^13 mod 497 = 445.
This calculator and the % operator in JavaScript
| a | n | a mod n here | a % n in JavaScript |
|---|---|---|---|
| 10 | 3 | 1 | 1 |
| -7 | 3 | 2 | -1 |
| 7 | -3 | -2 | 1 |
| -7 | -3 | -1 | -1 |
| 10 | -3 | -2 | 1 |
| -1 | 3 | 2 | -1 |
| -5 | 12 | 7 | -5 |
| 5.5 | 2 | 1.5 | 1.5 |
| -5.5 | 2 | 0.5 | -1.5 |
| 0.3 | 0.1 | 0 | 0.09999999999999998 |
| 9007199254740993 | 10 | 3 | 2 |
| 1234567890123456789 | 97 | 10 | 86 |
The two agree when a and n are positive. JavaScript’s % takes the sign of the dividend and works on binary doubles, which is why the last two rows and 0.3 % 0.1 differ.
Clocks, angles and time as modulo questions
| Question | Calculation | Result |
|---|---|---|
| 13:00 on a 12-hour clock | 13 mod 12 | 1 |
| 25 hours after 12 o’clock | 25 mod 12 | 1 |
| 5 hours before 12 o’clock | -5 mod 12 | 7 |
| An angle of 725 degrees | 725 mod 360 | 5 |
| A compass heading of -30 degrees | -30 mod 360 | 330 |
| Seconds left over in 1,000,000 seconds | 1000000 mod 60 | 40 |
| Odd or even: -3 | -3 mod 2 | 1 |
A result of 0 for n = 2 means even and 1 means odd, for negative numbers too.
How to use this tool
- Pick the operation: a mod n, (a + b) mod n, (a × b) mod n or aᵇ mod n.
- Enter the numbers. a and n can be negative or have decimals; a power needs whole numbers and an exponent of 0 or more.
- Read the result, the quotient and the line a = q × n + r underneath it.
- A zero divisor, an empty field or text that is not a number shows a message next to the field and no result.
Frequently asked questions
- What is -7 mod 3?
- 2 here. The calculator uses floored modulo, a − n × ⌊a ÷ n⌋, whose result has the sign of the divisor, as Python’s % does. JavaScript’s % takes the sign of the dividend and gives -1 for -7 % 3; the MDN page calls it a remainder operator and gives ((n % d) + d) % d for a true modulo.
- How do I calculate modulo by hand?
- Divide, round down, multiply back and subtract. 17 mod 5: 17 ÷ 5 = 3.4, which rounds down to 3, and 17 − 5 × 3 = 2. For a negative dividend, round down means towards minus infinity: -13 ÷ 5 = -2.6 rounds to -3, and -13 − 5 × (-3) = 2.
- How do I check whether a number is even or odd?
- Take it mod 2: 0 means even and 1 means odd. 10 mod 2 = 0, 17 mod 2 = 1, and -3 mod 2 = 1, so negative numbers work because the result takes the sign of the divisor 2.
- Why is 0.3 mod 0.1 equal to 0 here but 0.09999999999999998 in code?
- The two numbers are read as exact decimals here: 0.3 is 3 tenths and 0.1 is 1 tenth, so 3 tenths divide by 1 tenth exactly with nothing left. JavaScript stores them as binary fractions that are slightly off, and its 0.3 % 0.1 returns 0.09999999999999998.
- What happens when the divisor is 0 or the numbers are huge?
- A zero divisor shows The divisor cannot be zero. and no result. A value outside about 1e-300 to 1e300 is refused as outside the range the tool reads, and a form like 1e3 is read as 1000. The share link carries both fields (?a=17&b=5); a link value such as 1,000 is shown with a message instead of a result. A huge power is fine: aᵇ mod n works by repeated squaring, so 2^(10^50) mod 7 = 2 comes back at once, with an exponent of up to 200 digits.
- How is modulo used for clocks and angles?
- It wraps a number into a fixed range. 13 mod 12 = 1 turns 13:00 into 1 o’clock, -5 mod 12 = 7 is 5 hours before 12 o’clock, 725 mod 360 = 5 reduces an angle of 725 degrees, and -30 mod 360 = 330 turns a heading of -30 degrees into 330.
Related Tools
View all toolsLong Division Calculator
Long division of two numbers with every step shown: quotient, remainder, exact fraction, the decimal with its repeating digits marked, and the working.
Scientific Notation Calculator
Convert one number, such as 0.000123 or 1.2e-4, to scientific, standard decimal and engineering notation as you type. It does no arithmetic.
Sales Tax Calculator
Add sales tax to a price to get the total, or remove it from a total to find the price before tax. Enter the amount and your rate; tax is rounded to the cent.
Permutation and Combination Calculator
Counts permutations nPr and combinations nCr for n and r up to 1000, with or without repetition. Every digit is exact and the formula is worked through.
Derivative Calculator
Enter f(x) and a point x = a to get f′(a), f(a), the tangent line and its graph, plus the formula for f′(x). Flags corners, jumps and vertical tangents.
Scientific Calculator
Keypad or keyboard calculator with brackets, sin, cos, tan and their inverses in degrees or radians, log, ln, √, x^y, 10ˣ, memory and Ans, in the usual order.