🏆 US-Registered Digital Marketing Agency Trusted by 200+ brands · USA · UK · Canada · AUS
Advertisement
Advertisement
NUMBER THEORY

Modulo Calculator — remainder vs. true modulo, both shown

Find a mod n with negative operands handled properly, showing the truncated and floored results side by side.

The number being divided. Negative values are where the two conventions disagree.
The number you are dividing by. It cannot be zero.
Mathematical modulo (floored)
0
 
0
Truncated remainder (JS %)
0
Euclidean modulo
0
Floored quotient
0
Truncated quotient
Tip: JavaScript's % operator is a remainder, not a modulo. For negative dividends it returns a negative result, which is almost never what a wrap-around index needs.
Advertisement

The modulo calculator above answers "what is a mod n" and then does the thing most modulo calculators leave out: it shows you all three answers your programming language might give when the dividend is negative, and explains which one you actually want. For positive numbers every convention agrees and there is nothing to discuss. For negative numbers, −17 mod 5 is 3 in Python, −2 in JavaScript, and 3 under the Euclidean definition, and that discrepancy is a real source of bugs.

Arb Digital builds free tools with the edge cases handled rather than hidden, because the edge case is usually the reason someone reached for a calculator in the first place. If you want the whole-number factor structure behind these divisions, the prime factorization calculator covers it.

What This Modulo Calculator Does

Enter a dividend and a modulus and it returns four numbers. The headline result is the floored modulo, the mathematical convention where the result always takes the sign of the modulus. Alongside it sits the truncated remainder, which is what JavaScript, C, C++, Java, Go, and Rust produce with the % operator. The Euclidean modulo is a third convention where the result is always non-negative regardless of either sign. The two quotients show the division each convention implies, so you can verify that the identity a = n × q + r holds in every case.

A modulus of zero is refused with an explanation. Division by zero has no quotient, so it has no remainder either — languages that return NaN here are reporting the absence of an answer, not producing one.

How to Use It

  1. Enter the dividend — the value you are reducing, which can be negative.
  2. Enter the modulus — the value you are dividing by. Try a negative modulus too; the conventions diverge there as well.
  3. Read the headline floored result if you want the mathematical answer used in number theory and in Python.
  4. Read the truncated remainder if you are checking what a C-family language will return.
  5. Check the identity in the steps panel, which reconstructs the dividend from each quotient and remainder pair.

The Definition: Quotient and Remainder

Division with remainder splits a into a whole-number quotient q and a remainder r such that a = n × q + r. That equation alone does not pin down a unique answer, because different choices of q give different r. Every convention below satisfies the identity; they differ only in how q is rounded.

Truncated division rounds q toward zero. For −17 ÷ 5, that gives q = −3, so r = −17 − (5 × −3) = −17 + 15 = −2. The remainder takes the sign of the dividend. This is what the C standard specified and what most C-descended languages inherited, including JavaScript.

Floored division rounds q down toward negative infinity, and it is the convention behind the mod function as Wolfram MathWorld defines it. For −17 ÷ 5, that gives q = −4, so r = −17 − (5 × −4) = −17 + 20 = 3. The remainder takes the sign of the modulus. This is the mathematical convention, used by Python, Ruby, and Haskell, and it is the one that makes modular arithmetic behave consistently.

Euclidean division chooses q so that r is always non-negative, whatever the signs. For −17 and 5 it agrees with floored division and gives 3; for −17 and −5 it gives 3 where floored division gives −2. Wolfram MathWorld's entry on congruence covers the equivalence-class view that underpins all of them.

Advertisement

Why the JavaScript Result Causes Bugs

The classic case is wrapping an index around an array. You have a carousel of 5 slides and you want the previous slide from index 0. Writing (0 - 1) % 5 in JavaScript gives −1, not 4, and the lookup fails. The fix is to force the result positive: ((i % n) + n) % n. That expression takes the truncated remainder, adds the modulus to lift any negative into range, and reduces again in case it was already positive.

The same problem appears in circular buffers, hash tables where the hash can be negative, angle normalisation into 0–359 degrees, and clock arithmetic that crosses midnight backwards. In every case the intent is the floored modulo, and in every case the C-family % quietly gives something else for exactly the inputs that are hardest to test.

It is worth noting that this is not a bug in JavaScript. Truncated remainder is a deliberate, documented choice with its own justification — it keeps the identity that changing the sign of the dividend flips the sign of the result, which matters in some numerical work. The bug is assuming one convention while using another.

Modular Arithmetic: Clocks, Days, and Wrapping

Modulo is how any cyclical quantity is computed. A 12-hour clock is arithmetic mod 12: five hours after 9 o'clock is (9 + 5) mod 12 = 2. Days of the week are mod 7: one hundred days after a Wednesday lands on (3 + 100) mod 7 = 5, a Friday, counting Sunday as 0. Angles wrap mod 360, and bytes wrap mod 256.

Two numbers that leave the same remainder are called congruent modulo n, written a ≡ b (mod n). This is what makes the arithmetic useful: you can add, subtract, and multiply congruences freely, so a huge calculation can be reduced at every step instead of at the end. Computing 7¹⁰⁰ mod 13 does not require computing 7¹⁰⁰ at all — reduce after each multiplication and the numbers never exceed 13.

The most familiar practical use is check digits. ISBN-13, the barcode standard, and the Luhn algorithm behind credit card numbers all compute a weighted sum and take it mod 10 or mod 11 to produce a digit that catches most typing errors. Every time a form rejects a mistyped card number before contacting the bank, a modulo operation did it.

Even, Odd, Divisible: The Everyday Uses

The most common use of modulo is testing divisibility. If a mod n is 0, then n divides a exactly. Testing mod 2 separates even from odd, which is how alternating table row colours are usually implemented. Testing mod 15 alongside mod 3 and mod 5 is the whole of the FizzBuzz exercise.

Modulo also extracts digits and splits units. The last digit of a number is n mod 10; converting 200 minutes to hours and minutes is 200 ÷ 60 = 3 with 200 mod 60 = 20 left over. Distributing items into groups uses the same pair: how many full groups, and how many left over. If you need to distribute a total across parts proportionally rather than by remainder, the ratio calculator handles that split, and the percentage calculator covers proportional shares.

One caution on the divisibility test: with a negative dividend, a mod n is 0 under every convention when n divides a, so divisibility testing is safe regardless of language. It is only the non-zero remainders where the conventions part company — which is precisely why the bug is so easy to miss in testing.

Negative Modulus, and Other Corner Cases

A negative modulus is unusual but legal. Under floored division the result takes the sign of the modulus, so 17 mod −5 gives −3. Under truncated division it takes the sign of the dividend, giving 2. Under the Euclidean convention it is always non-negative, giving 2 here. If you find yourself with a negative modulus in real code, the safest move is usually to take its absolute value first and be explicit about what you meant.

Non-integer operands are another divergence. JavaScript's % accepts floats and returns a float remainder, so 5.5 % 2 is 1.5. Many languages refuse this entirely. This calculator works in whole numbers, since modular arithmetic is defined on integers and the float behaviour is better thought of as a separate operation.

Need custom tools or a fast, well-built website?

Arb Digital builds dependency-free web tools and the sites they live on, designed to load quickly and rank in search. Browse the free library, or start a conversation.

Browse All Free Tools Talk to Arb Digital

Common Mistakes to Avoid

  • Assuming % is a modulo. In JavaScript, C, Java, and Go it is a truncated remainder, and it returns negative values for negative dividends.
  • Wrapping an index with a bare %. Use ((i % n) + n) % n so the result is always in range.
  • Porting code between Python and JavaScript unchanged. Python floors, JavaScript truncates, and only negative operands reveal the difference.
  • Using a modulus of zero. There is no quotient and therefore no remainder; the operation is undefined, not zero.
  • Testing only positive inputs. The conventions agree completely when both operands are positive, so a passing test suite proves nothing about the negative case.

Related Free Tools From Arb Digital

Use the prime factorization calculator for the divisor structure behind these remainders, the prime number checker for primality, and the LCM and GCF calculator for common factors and multiples. The number base converter uses repeated modulo internally, and the exponent calculator helps with the powers that appear in modular exponentiation. The free online tools hub lists everything else.

Frequently Asked Questions

What does modulo mean?

It is the remainder left after dividing one number by another. 17 mod 5 is 2, because 5 goes into 17 three times with 2 left over.

What is -17 mod 5?

Under the mathematical, floored convention it is 3, which is what Python and Ruby return. JavaScript, C, and Java return -2 instead, because they truncate the quotient toward zero rather than flooring it.

Is JavaScript's % operator a modulo?

No, it is a remainder. It takes the sign of the dividend, so a negative input gives a negative result. To get a true modulo in JavaScript, use ((a % n) + n) % n.

Why do languages disagree about negative modulo?

Because they round the quotient differently. Truncated division rounds toward zero and floored division rounds toward negative infinity, and both satisfy the identity that dividend equals modulus times quotient plus remainder.

What is Euclidean modulo?

A convention where the remainder is always non-negative, whatever the signs of the operands. It agrees with floored division for a positive modulus and differs when the modulus is negative.

What happens when the modulus is zero?

The operation is undefined. Division by zero has no quotient, so there is no remainder either, and the calculator returns an explanation rather than a number.

What is modulo used for?

Anything cyclical or divisibility-based: clock and calendar arithmetic, wrapping array indices, hash table bucketing, alternating row styles, and the check digits that validate ISBNs and card numbers.

This tool is provided for educational and general reference use. Always confirm the behaviour of your specific language or platform in its own documentation.

Advertisement
Advertisement

Take it further

Arb Digital assistant

👋 Hey! Want to grow your business? Ask me anything — a free marketing proposal is on the table!