Modulo
calculator.
Find a mod b the way Python, JavaScript, Excel and other languages do, on a clock.
Python, Rust rem_euclid, Ruby, Excel MOD and Google Sheets MOD give 3; JavaScript, Java, C and C++, C#, Go and Rust give −2. Floored rules use the quotient −4 and truncated rules use −3, the whole number nearer zero.
Your numbers in each language
| Language | Result | Type this |
|---|---|---|
Python %-17 % 5 | 3 | -17 % 5 |
JavaScript %-17 % 5 | −2 | -17 % 5 |
Java %-17 % 5 | −2 | -17 % 5 |
C and C++-17 % 5 | −2 | -17 % 5 |
C# %-17 % 5 | −2 | -17 % 5 |
Go-17 % 5 | −2 | -17 % 5 |
Rust %-17 % 5 | −2 | -17 % 5 |
Rust rem_euclid(-17).rem_euclid(5) | 3 | (-17).rem_euclid(5) |
Ruby %-17 % 5 | 3 | -17 % 5 |
Excel MOD=MOD(-17, 5) | 3 | =MOD(-17, 5) |
Google Sheets MOD=MOD(-17, 5) | 3 | =MOD(-17, 5) |
To get a remainder that is never negative in a truncating language, use ((a % b) + b) % b for a positive b. References: Python Language Reference 6.7; ECMAScript Number::remainder; Java Language Specification 15.17.3; ISO C11 6.5.5; C# remainder operator; Go specification, arithmetic operators; Rust reference, arithmetic operators; Rust standard library, rem_euclid; Ruby Integer#%; Microsoft MOD function; Google Sheets MOD.
On a clock
Counting −17 steps backward around a clock with 5 positions from 0 stops at 3. Python % reports that position, 3.
Remainders near a
| n | n mod 5 |
|---|---|
| −20 | 0 |
| −19 | 1 |
| −18 | 2 |
| −17(yours) | 3 |
| −16 | 4 |
| −15 | 0 |
| −14 | 1 |
| −13 | 2 |
Working
- 1Divide−17 ÷ 5 = −3.4
- 2Choose the whole-number quotientFloored: q = floor(a ÷ b), rounding down, so q = −4
- 3Remainder r = a − q × b−17 − (−4) × 5 = −17 − (−20) = 3
- 4Check a = q × b + r−4 × 5 + 3 = −17
- 5Python % returns−17 mod 5 = 3
Many numbers mod 5
Type or paste a column of numbers. Each one is divided by the modulus above under all three rules.
| # | a |
|---|---|
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 |
Remainders
| a | Truncated | Floored | Euclidean |
|---|---|---|---|
| −3 | −3 | 2 | 2 |
| −1 | −1 | 4 | 4 |
| 0 | 0 | 0 | 0 |
| 4 | 4 | 4 | 4 |
| 13 | 3 | 3 | 3 |
| 26 | 1 | 1 | 1 |
How it’s worked out
a mod b is the remainder after you take as many whole b’s out of a as you can. Every answer fits this line, where q is a whole number called the quotient and r is the remainder:
For 17 mod 5, five fits into 17 three times, so q = 3 and r = 2. Check: 3 × 5 + 2 = 17.
Negative numbers have more than one answer. The line above works for several whole numbers q, so each rule picks q its own way:
- Floored: divide, then round q down. The remainder has the same sign as b. Python, Ruby, Excel and Google Sheets use this.
- Truncated: divide, then cut off the decimals, so q moves toward zero. The remainder has the same sign as a. C, C++, Java, JavaScript, C#, Go and Rust’s % use this.
- Euclidean: pick q so the remainder is never negative, 0 ≤ r < |b|. Rust’s rem_euclid and most number theory books use this.
Here is 17 and 5 with every mix of signs:
| Sum | Floored | Truncated | Euclidean |
|---|---|---|---|
| 17 mod 5 | 2 | 2 | 2 |
| −17 mod 5 | 3 | −2 | 3 |
| 17 mod −5 | −3 | 2 | 2 |
| −17 mod −5 | −2 | −2 | 3 |
Each language states its rule in its reference. For −17 and 5 they give:
| Language reference | Rule | Result |
|---|---|---|
| Python % | Floored | 3 |
| Excel MOD | Floored | 3 |
| JavaScript % | Truncated | −2 |
| Java % | Truncated | −2 |
| Rust rem_euclid | Euclidean | 3 |
Decimals are exact here. Both numbers are scaled up to whole numbers, divided, and scaled back. So 0.3 mod 0.1 is 0, and a 23-digit number keeps every digit: 12,345,678,901,234,567,890,123 mod 97 = 19.
Worked examples
Porting Python code to JavaScript
Priya moves a calendar script from Python to JavaScript. In Python, −17 % 5 gives 3. In JavaScript the same line gives −2, and her array index goes negative.
The reason is the quotient: −17 ÷ 5 = −3.4. Python rounds it down to −4, so it measures from −20 and the gap to −17 is 3. JavaScript cuts it to −3 and measures from −15, which leaves −2. The fix in JavaScript is ((a % b) + b) % b, which gives 3 for these numbers.
What time is it 100 hours from now?
A server job starts at midnight and runs for 100 hours. A day has 24 hours, so take 100 mod 24. Twenty-four fits 4 times with 4 left over. The job ends at 4:00 after 4 full days.
On the clock picture, the hand goes round 4 times and stops at 4. Move the slider to see how each extra hour moves it one step.
What weekday is 100 days from Monday?
Weeks repeat every 7 days, so only 100 mod 7 matters. That is 2, after 14 full weeks. Count 2 days on from Monday and you land on Wednesday.
Reading your result
The big number is the remainder your chosen language or rule returns. The line under it writes out a = q × b + r, so you can check it by hand.
The tiles give all three rules at once. If they match, any language will agree. That happens when a and b are both positive, or when b divides a.
The clock appears when b is a whole number from 2 to 24. Count a steps from 0, going backward when a is negative. The hand stops on the Euclidean remainder. A truncated answer such as −2 is the same spot, counted back from b. For decimals or bigger numbers, a number line shows the multiples of b. The shaded gap from the chosen multiple to a is the remainder.
The language table shows exactly what to type in each language and what comes back. For decimals, real languages work in binary and can show a tiny error. The values here are exact.
Common mistakes
- Testing for odd numbers with n % 2 == 1. In JavaScript, Java or C, −7 % 2 is −1, not 1, so negative odd numbers slip through. Test n % 2 != 0 instead.
- Expecting a positive answer from a negative modulus. In Python, 17 % −5 is −3, because the floored remainder takes the sign of b.
- Using % on decimals and trusting every digit. 0.3 % 0.1 gives 0.09999999999999998 in most languages, not 0. Round the result, or work in whole units such as cents.
- Dividing by zero. a mod 0 has no answer. Most languages throw an error or return NaN.
- Mixing up the quotient and the remainder. In 100 mod 24, the 4 is full days and the 4 is the hour on the clock.
Questions people ask
Sources
- Raymond T. Boute, The Euclidean definition of the functions div and mod, ACM Transactions on Programming Languages and Systems 14(2), 1992, pp. 127 to 144
- Python Software Foundation, The Python Language Reference, section 6.7 (binary arithmetic operations)
- ISO/IEC 9899:2011, Programming languages: C, section 6.5.5 (multiplicative operators)
- Donald E. Knuth, The Art of Computer Programming, Volume 1, 3rd edition (Addison-Wesley, 1997), section 1.2.4 (integer functions and elementary number theory)
- ECMAScript Language Specification, Number::remainder
- Java Language Specification, section 15.17.3 (remainder operator %)
- Microsoft, MOD function
- Rust standard library, i32::rem_euclid