Modulo calculator — remainders for negative and huge numbers

Enter a dividend and a divisor to get a mod n under all three common conventions, floored, truncated and Euclidean, with the quotient and a check line.

Live demo · real result from this tool
Floored modulo (maths, Python %)-17 mod 5 = 3
Floored quotient
-4
Truncated remainder (C, Java, JavaScript %)
-2
Truncated quotient
-3
Euclidean remainder (always ≥ 0)
3

Check

-17 = 5 × -4 + 3-17 = 5 × -3 + -2  (truncated)

With negative numbers the conventions differ: floored modulo takes the sign of the divisor, the truncated remainder takes the sign of the dividend.

Processed on your device — nothing is uploaded.

How to use the modulo calculator

  1. Type the number being divided in Dividend a.
  2. Type the number you divide by in Divisor n.
  3. Read the top line, Floored modulo (maths, Python %), which shows a mod n = r.
  4. Compare it with the Truncated remainder and the Euclidean remainder if you are working with negative numbers or code.
  5. Use the Check box to confirm a = n × q + r.

What it does and when to use it

The modulo operation gives the remainder after division. 17 mod 5 = 2 because 17 = 5 × 3 + 2. You use it for clock times (15:00 is 3 o’clock because 15 mod 12 = 3), for working out days of the week, for checking even and odd numbers, and for check digits.

With positive numbers every convention agrees. With negatives they do not, and that catches out many programmers and students. This tool lays the three rules next to each other so you can pick the one your language or textbook uses. For whole-number division in binary, see the binary calculator; for common factors, the GCD and LCM calculator uses repeated mod steps.

How it works

Every rule writes a = n × q + r. They differ only in how the quotient q is chosen from a ÷ n:

Rule Quotient q Sign of r
Floored (maths, Python) floor(a ÷ n), rounded down same as n
Truncated (C, Java, JavaScript) a ÷ n with the decimals cut off same as a
Euclidean floor if n > 0, ceiling if n < 0 never negative

Then r = a − n × q. The numbers are stored as exact fractions, so long dividends and decimals do not lose precision.

Step by step for −17 mod 5:

  1. Divide: −17 ÷ 5 = −3.4.
  2. Floored: round down to −4. r = −17 − 5 × (−4) = −17 + 20 = 3.
  3. Truncated: cut off the decimals to −3. r = −17 − 5 × (−3) = −17 + 15 = −2.
  4. Euclidean: n is positive, so it matches floored: 3.

The tool shows -17 mod 5 = 3 and the check -17 = 5 × -4 + 3.

Worked examples

Positive numbers. 17 and 5 give 17 mod 5 = 2, with quotient 3. All three rules agree.

Negative dividend. −17 and 5 give 3 floored, but Truncated remainder (C, Java, JavaScript %): -2.

Negative divisor. 17 and −5 give 17 mod -5 = -3 floored, 2 truncated, and a Euclidean remainder of 2.

A 30-digit number. 123456789012345678901234567890 mod 97 gives mod 97 = 52, exactly.

Limits and tips

  • The Euclidean remainder is always 0 or more, which makes it safe for array indexes and wrapping around a list.
  • In JavaScript you can get the floored result with ((a % n) + n) % n when n is positive.
  • To turn the remainder into a fraction such as 17/5 = 3 2/5, use the fraction calculator.
  • Nothing leaves your browser.

Frequently asked questions

Why do Python and JavaScript give different answers for −17 % 5?
Python uses floored division, so −17 % 5 is 3. JavaScript, C and Java truncate towards zero, so −17 % 5 is −2. Both satisfy a = n × q + r; they just pick a different quotient. The tool shows both.
Which answer should I use in maths class?
Usually the floored or Euclidean one, which is never negative when the divisor is positive. Clock arithmetic and modular arithmetic in number theory use this convention.
Can I use decimals?
Yes. 7.5 mod 2 = 1.5, because 7.5 = 2 × 3 + 1.5. The tool works with exact decimals, so you do not get answers like 1.4999999.
How big can the numbers be?
The dividend can be as long as you like within reason; a 30-digit number is handled exactly. Ordinary calculators lose the last digits of numbers that long.
What about dividing by zero?
A divisor of 0 has no remainder, so the tool shows a message instead of a result.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close