Modulo Of Negative Numbers - Floor, Truncated, and Euclidean

Use this modulo of negative numbers calculator to see the floor-mod result, the truncated (C/JS) result, and the matching integer quotient.

Updated: June 18, 2026 • Free Tool

Modulo Of Negative Numbers

Any integer, positive or negative. The number on the left side of mod.

Any non-zero integer. Most examples use a positive divisor; negative divisors are supported but produce a different sign rule.

Pick the rule used to define the primary result. The truncated value is always shown next to it for comparison.

Results

Primary remainder
0
Truncated remainder (C/JS) 0
Integer quotient 0
Sign rule 0

What Is Modulo Of Negative Numbers?

The modulo of negative numbers calculator is a focused math tool that takes any integer dividend and divisor and shows the remainder using three common conventions: the floored remainder used in mathematics and Python, the truncated remainder used in C, Java, and JavaScript, and the Euclidean remainder that is always non-negative. Most calculators only show one of these answers, which is why -7 mod 3 looks wrong to people who switch between languages. This page shows all three at once so the sign rule is visible at a glance.

  • Translating between languages: Move an expression from Python to JavaScript (or back) without surprising negative remainders, by reading both conventions side by side.
  • Verifying clock arithmetic: Check 12-hour clock math where the dividend can wrap negative; confirm the floored remainder matches the clock hand.
  • Debugging array indexing: When an index is allowed to go negative, the floored vs truncated distinction decides whether -1 wraps to the last element or returns out of range.
  • Homework and exam practice: Compute -12 mod 5, -3 mod 7, or 7 mod -3 by hand and confirm against the quotient and remainder.

Modular arithmetic is built around remainders, and the only real disagreement between definitions is what to do when the dividend or divisor is negative. Math and Python choose the floored definition so the result lands in [0, n) for a positive divisor. C, Java, and JavaScript choose the truncated definition, putting -7 mod 3 at -1 instead of 2. Both are correct.

If both numbers are positive, the Modulo Calculator handles the basic remainder, modular inverse, and modular exponentiation in one form.

How Modulo Of Negative Numbers Works

The calculator implements all three definitions directly so the numbers you see match what you would compute by hand. What changes between the three modes is which integer quotient k is paired with the dividend; the remainder follows from there.

r = a - n * floor(a / n) (Python / math, floored) r = a - n * trunc(a / n) (C, Java, JavaScript, truncated) r = ((a mod n) + |n|) mod |n| (Euclidean, always non-negative)
  • a: The dividend. Any integer, including negative values.
  • n: The divisor. Any non-zero integer, including negative values.
  • k: The integer quotient chosen by the convention. Floor for Python, trunc for C/JS, and a value that keeps r non-negative for Euclidean.
  • r: The remainder. Always satisfies a = n*k + r, and the range depends on the chosen convention.
  • mode: The dropdown. 'floored' uses floor, 'truncated' uses trunc, and 'euclidean' uses the always-non-negative variant.

The output panel reports the primary remainder, the truncated remainder, the integer quotient, and a one-line sign rule. Floored or Euclidean gives a non-negative answer for a positive divisor; Truncated keeps the sign of the dividend, matching what C or JavaScript would print.

For negative divisors, the floored convention puts the remainder in (n, 0]. The calculator handles that automatically, so 7 mod -3 shows primary remainder -2 and integer quotient -3.

Example: -7 mod 3 in three conventions

a = -7, n = 3

floored: k = floor(-7/3) = -3, r = -7 - 3*(-3) = 2. truncated: k = trunc(-7/3) = -2, r = -7 - 3*(-2) = -1. euclidean: r = ((-7 mod 3) + 3) mod 3 = 2.

Floored remainder = 2. Truncated remainder = -1. Euclidean remainder = 2. Integer quotient (floored) = -3.

Floored and Euclidean agree; truncated carries the negative sign from -7. The integer quotient makes the split explicit: floored drops to -3, truncated stays at -2.

According to Wikipedia, the floored variant of the modulo operation returns a remainder in [0, n) for a positive divisor, while the truncated variant used in C, Java, and JavaScript keeps the sign of the dividend.

Once you have a remainder for a single mod step, the Is Modulo Associative Calculator shows whether chaining (a mod n) mod m gives the same answer as a mod (n mod m) - a question that only shows up once you start composing these operations.

Key Concepts Explained

Four small ideas explain every behavior you see on this modulo of negative numbers calculator. Keeping them straight is the difference between guessing the sign and predicting it.

Floored modulo

The mathematical definition. Take the floor of a/n, multiply by n, and subtract from a. For a positive divisor the result is always in [0, n), so -7 mod 3 = 2.

Truncated modulo

The definition C, Java, and JavaScript use. Take the truncated quotient (drop the fractional part toward zero), multiply by n, and subtract. The result keeps the sign of the dividend, so -7 mod 3 = -1.

Euclidean remainder

A version that is always non-negative regardless of sign. The result lands in [0, |n|), which makes it the safest choice when you only care about wrap-around behavior like clocks or hash buckets.

Integer quotient

The integer k that satisfies a = n*k + r. Each convention picks a different k for the same pair (a, n), and that choice is the source of every sign difference between the three remainders.

Most negative-remainder surprises come from swapping languages without changing definitions. The integer quotient k is where to look first: floored and truncated pick a different k for the same a and n, and that single choice flips the sign when the dividend is negative.

The Quotient Calculator computes integer quotients for any pair of integers, which is useful when you want to verify k by hand before trusting a mod result.

How to Use This Calculator

Three inputs produce all three remainders. Type the dividend, choose the divisor, and pick the convention you want as the primary answer.

  1. 1 Enter the dividend: Type the dividend (a). Negative values are allowed and encouraged, since this calculator exists specifically to handle them.
  2. 2 Enter the divisor: Type the divisor (n). Zero is rejected with a clear error so you cannot accidentally compute a mod 0.
  3. 3 Pick the convention: Use the dropdown to choose Floored, Truncated, or Euclidean. The default is Floored so the page matches Python and standard math notation.
  4. 4 Read the primary remainder: Look at the primary value first. This is the answer under the convention you picked.
  5. 5 Compare with the truncated value: Glance at the truncated remainder row to see what C, Java, or JavaScript would print for the same inputs. Use it as a sanity check when porting code.
  6. 6 Verify by hand with the quotient: Multiply the divisor by the integer quotient, add the primary remainder, and confirm you get the dividend back. That single check covers sign, magnitude, and convention at once.

Try -7 mod 3 with the default Floored convention: dividend -7, divisor 3, dropdown on Floored. The primary remainder reads 2, the truncated remainder -1, and the integer quotient -3. Switch to Truncated and the primary flips to -1 with quotient -2. Now switch a to 7 and n to -3: the floored remainder drops to -2 with quotient -3, and the truncated column reads 1 because 7 - (-3)(-2) = 1.

When you only need the truncated sign-of-dividend answer and not the floored or Euclidean views, the Remainder Calculator gives you that one number with a clean division-style layout that mirrors long-division homework.

Benefits of Using This Calculator

Negative modulo wastes an afternoon when the sign is wrong. Putting all three conventions on one page turns it into a five-second check.

  • Three conventions at a glance: Floored, truncated, and Euclidean answers sit next to each other so the sign rule is visible, not memorized.
  • Quotient for hand verification: Divisor times quotient plus remainder should equal the dividend; the panel shows k so the check is one multiplication away.
  • Negative divisors handled correctly: 1 mod -5 and 7 mod -3 produce the right floored and truncated split without any extra work.
  • Safer language ports: A side-by-side comparison of the floored and truncated answer removes the most common Python-to-JavaScript (or back) porting bug.

The biggest win is dropping the language-by-language memorization step. Once you have seen the floored and truncated answers for -7 mod 3, the same approach works for any negative-dividend case.

If the negative-modulo question turns out to be one step inside a system of congruences x congruent to a mod n, the Chinese Remainder Calculator solves the whole system and reports the canonical x, so you do not have to compute the remainders one at a time.

Factors That Affect Your Results

Five things decide what the calculator prints. Knowing them keeps the answers predictable.

Sign of the divisor

Negative divisors flip the interval. With floored modulo, the result lives in (n, 0] for n negative, which surprises users who only know positive divisors.

Magnitude ratio

When the divisor is positive and |a| is smaller than |n|, both conventions coincide at a because the integer quotient is zero. Flip the divisor negative and 1 mod -5 floored becomes -4 instead of 1.

Exact multiple edge case

If a is an exact multiple of n, every convention returns 0. The calculator reports quotient k = a/n and remainder 0 so the user sees the division was exact.

Language convention

Different programming languages default to different rules. Python, Ruby, and Lua use floored; C, Java, and JavaScript use truncated. The same expression in two languages can return two different numbers.

Convention dropdown

Choosing Floored, Truncated, or Euclidean changes the integer quotient and therefore the sign of the primary remainder. The truncated value is always shown next to the primary value for a quick comparison.

  • The calculator works only on integers. Floating-point dividends like -7.5 mod 3 are not supported; use a dedicated floating-point tool instead.
  • Truncated mode follows the sign-of-dividend rule used by C, Java, and JavaScript. It does not match languages that define modulo differently, so double-check the sign rule for the target system.

Even with all three answers visible, the only definitive answer is the one tied to a specific language. If you need the JavaScript answer, the truncated column gives you -1 without leaving the browser.

The sign of the divisor and dividend together decide the interval. For positive n, floored and Euclidean answers always sit in [0, n); for negative n, the floored interval becomes (n, 0].

According to Python documentation, the % operator uses floored division, so -7 % 3 evaluates to 2 and 7 % -3 evaluates to -2.

According to MDN, JavaScript's remainder operator % is defined so the result carries the sign of the dividend, which is why -7 % 3 evaluates to -1 in any browser.

Beyond sign conventions, the Uses Of Modulo Calculator lists where negative modulo actually shows up in practice - hash tables, clock arithmetic, calendar offsets, and the wrap-around logic that depends on the floored vs truncated rule this page exposes.

modulo of negative numbers calculator showing the floor-mod, truncated, and Euclidean remainders plus the integer quotient for negative dividends and divisors.
modulo of negative numbers calculator showing the floor-mod, truncated, and Euclidean remainders plus the integer quotient for negative dividends and divisors.

Frequently Asked Questions

Q: What is modulo of negative numbers?

A: Modulo of negative numbers is the remainder when a negative integer is divided by another integer. The answer depends on the convention: math and Python return a non-negative remainder for a positive divisor, while C, Java, and JavaScript return a remainder that carries the sign of the dividend. The calculator shows all three.

Q: How do you calculate modulo with a negative dividend?

A: Divide the dividend by the divisor, take the floor (math/Python) or the trunc (C/JavaScript) of that quotient, multiply by the divisor, and subtract. For -7 mod 3 with floor you get -7 - 3*(-3) = 2; with trunc you get -7 - 3*(-2) = -1.

Q: Why does -7 mod 3 give different answers in Python vs JavaScript?

A: Python uses floored division for %, so -7 % 3 returns 2. C, Java, and JavaScript use truncated division, so -7 mod 3 returns -1 in those languages. Both answers are correct under their own definitions; the difference is whether the integer quotient is -3 (floor) or -2 (trunc).

Q: What is the difference between floored and truncated modulo?

A: Floored modulo uses the floor of a/n, so the result is always in [0, n) for a positive divisor. Truncated modulo uses the trunc of a/n, so the result keeps the sign of the dividend. Euclidean modulo is a third option that always returns a non-negative result regardless of signs.

Q: Can the divisor in a modulo be negative?

A: Yes. With a negative divisor, the floored remainder lands in (n, 0]. For example, 7 mod -3 floored is -2 because floor(7 / -3) = -3 and 7 - (-3)(-3) = -2. The truncated answer for 7 mod -3 is 1 because trunc(7 / -3) = -2 and 7 - (-3)(-2) = 1, which carries the sign of the dividend (positive).

Q: How do you do modulo with negative numbers in Excel?

A: Excel's MOD function returns a result with the sign of the divisor, like Python's floored convention: =MOD(-7, 3) returns 2. To get the C/JavaScript answer instead, use =(-7) - 3*TRUNC((-7)/3), which truncates the quotient toward zero and returns -1.