MathsBasic Arithmetic

Modulo
calculator.

Find a mod b the way Python, JavaScript, Excel and other languages do, on a clock.

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:

a\displaystyle a=q×b+r\displaystyle {}= q \times b + r

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.
rfloored\displaystyle r_{\text{floored}}=a−b⌊ab⌋\displaystyle {}= a - b\left\lfloor \frac{a}{b} \right\rfloorrtruncated\displaystyle r_{\text{truncated}}=a−btrunc⁡(ab)\displaystyle {}= a - b\,\operatorname{trunc}\!\left(\frac{a}{b}\right)

Here is 17 and 5 with every mix of signs:

SumFlooredTruncatedEuclidean
17 mod 5222
−17 mod 53−23
17 mod −5−322
−17 mod −5−2−23

Each language states its rule in its reference. For −17 and 5 they give:

Language referenceRuleResult
Python %Floored3
Excel MODFloored3
JavaScript %Truncated−2
Java %Truncated−2
Rust rem_euclidEuclidean3

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.

Open this example in the tool

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.

Open this example in the tool

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.

Open this example in the tool

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

Python rounds the quotient down (floored division), giving q = −3 and r = 2, while JavaScript rounds it toward zero (truncated division), giving q = −2 and r = −1. Both satisfy −7 = q × 3 + r; they just choose different quotients.

Sources