Programming

Remainder of Negative Numbers: Why -7 mod 3 Is 2 or -1

The remainder of a negative number has two valid answers. Learn why -7 mod 3 gives 2 in Python and -1 in C, how floored and truncated division decide the sign, and which to use.

By Remainder Calculator Team

The remainder of a negative number has two valid answers, and the correct one depends on the definition of division being used. The expression -7 divided by 3 leaves a remainder of 2 under floored division and −1 under truncated division, so Python returns 2 and C returns −1 for -7 % 3. Both answers satisfy the division identity, which is why the disagreement is real and not a bug. The Remainder Calculator reports the non-negative mathematical result, and this guide explains where the second answer comes from and when each one is right.

Getting this right pays off in a few concrete ways. It kills the most common off-by-one error in modular code, keeps negative array indices wrapping the way you expect, and tells you which convention a math proof is leaning on. The whole disagreement comes down to one choice: which way you round the quotient. Fix that, and the remainder falls out with no ambiguity.

Three things drive every negative-remainder problem. The dividend may be negative. The divisor sets the sign of the floored answer. And the rounding rule breaks the tie between the two results. Ahead you’ll find the division identity, floored versus truncated rounding, the four sign cases, and how the modulo operator works in practice.

Why Does a Negative Number Have Two Remainders?

A negative number has two remainders because two rounding rules both satisfy the division identity. The identity dividend = divisor × quotient + remainder holds for more than one quotient when the dividend is negative, and each valid quotient produces a different remainder.

Dividing −7 by 3 allows two quotients:

  • A quotient of −3 gives 3 × (−3) = −9, and −7 − (−9) = 2. The remainder is 2.
  • A quotient of −2 gives 3 × (−2) = −6, and −7 − (−6) = −1. The remainder is −1.

Both equations hold. −7 = 3 × (−3) + 2, and −7 = 3 × (−2) + (−1). The identity by itself picks no winner. You need a second rule, and that rule is how the quotient gets rounded. That single distinction is what splits mathematics from most programming languages.

Floored Division Gives a Non-Negative Remainder

Floored division rounds the quotient toward negative infinity, and that pins the remainder to the sign of the divisor. With a positive divisor, the remainder never drops below zero.

Compute −7 mod 3 with floored division:

  1. Divide: −7 / 3 = −2.33.
  2. Floor: floor(−2.33) = −3, rounding down to the next lower integer.
  3. Multiply and subtract: −7 − 3 × (−3) = −7 + 9 = 2.

The remainder is 2. Number theory leans on this convention, since a remainder from 0 to divisor − 1 keeps every congruence class non-negative. Euclidean division makes it formal, and that link to Euclidean division and the floor function is why Python, Ruby and pure math all report 2.

Truncated Division Gives a Signed Remainder

Truncated division rounds the quotient toward zero, so the remainder inherits the sign of the dividend. Hand it a negative dividend and a negative remainder comes back.

Compute −7 mod 3 with truncated division:

  1. Divide: −7 / 3 = −2.33.
  2. Truncate: trunc(−2.33) = −2, dropping the decimal toward zero.
  3. Multiply and subtract: −7 − 3 × (−2) = −7 + 6 = −1.

The remainder is −1. C, C++, Java, JavaScript, Go and Rust all sit here, because truncation matches the integer division the hardware already does. The C99 standard nailed this down, so -7 % 3 returns −1 across the whole C family. Every expression in modulo across programming languages traces back to that one rounding choice.

The Four Sign Cases

The sign of the remainder depends on the signs of both operands and the rounding rule. The table lists all 4 combinations for divisor 3 and dividend 7.

ExpressionFloored (Python)Truncated (C)
7 % 311
-7 % 32−1
7 % -3−21
-7 % -3−1−1

Two rules explain the whole table. Under floored division the remainder tracks the sign of the divisor: a divisor of 3 gives non-negative results, a divisor of −3 gives non-positive ones. Under truncated division the remainder tracks the sign of the dividend instead, so a dividend of 7 stays non-negative and a dividend of −7 stays non-positive.

When both operands share a sign, as in 7 % 3 and -7 % -3, the two conventions still differ only when the dividend is negative. The expression -7 % -3 returns −1 in both, since flooring and truncating −2.33 toward each direction land differently but the arithmetic resolves to the same leftover.

Which Remainder Is Correct?

The mathematically preferred remainder is the non-negative one, so −7 mod 3 is 2. Euclidean division defines the remainder to satisfy 0 ≤ r < |divisor|, which rules out −1 as the canonical answer. A remainder of 0 is the single value every definition shares, which is what a remainder of zero means in every language, and it is why the divisibility rules and prime tests by division stay valid regardless of sign.

Programming, though, follows the language, not the math:

  • Choose the language default when the code stays in one language and negatives are rare.
  • Force the non-negative result when negatives wrap an index, an hour, or a hash bucket.
  • Match the reference language when porting an algorithm that assumes a specific sign.

The safe rule is to make the intent explicit rather than trust the default, because a single negative input can flip a result from 2 to −1 and silently corrupt every value downstream. Checking the result against the full long division method confirms which convention a piece of code follows.

Getting a Non-Negative Remainder in Code

The expression ((a % b) + b) % b returns a non-negative remainder in any truncating language. It adds the divisor to shift a negative result into range, then reduces once more to stay below the divisor.

Applying it to -7 % 3 in C or JavaScript:

  1. Inner remainder: -7 % 3 = −1.
  2. Add the divisor: −1 + 3 = 2.
  3. Outer remainder: 2 % 3 = 2.

The result is 2, matching Python. Java offers Math.floorMod(-7, 3) and Rust offers (-7).rem_euclid(3), both returning 2 directly. You need this correction for negative array indices, where -1 % length has to become length − 1 to land on the last element instead of an invalid slot.

Worked Examples

DividendDivisorFloored remainderTruncated remainder
−732−1
−154−1
−1042−2
−1563−3
8−5−23

Common Mistakes With Negative Remainders

Four errors cause most negative-remainder bugs:

  • Expecting -7 % 3 to return 2 in C or JavaScript, when both return −1.
  • Using a raw negative modulo as an array index, which points outside the array.
  • Assuming the math textbook answer and the code answer always agree, when they diverge on every negative dividend.
  • Forgetting that a negative divisor flips the sign under floored division.

Testing modular code with at least one negative dividend catches all 4 before they reach production. For positive divisors, faster mental shortcuts give the non-negative remainder directly, with no sign to correct.

Frequently Asked Questions

What is -7 mod 3?

The value of −7 mod 3 is 2 under the mathematical definition and −1 under truncated division. Python returns 2, while C, JavaScript and Java return −1, because they round the quotient differently.

Can a remainder be negative?

Yes, a remainder can be negative under truncated division, which most programming languages use. Under the mathematical Euclidean definition the remainder is always non-negative, so −7 mod 3 is 2.

How do I make a negative modulo positive?

Use the expression ((a % b) + b) % b to make a negative modulo positive. It shifts the result up by the divisor and reduces it back into the range 0 to one less than the divisor.

Why does Python return 2 but C returns -1?

Python returns 2 because it uses floored division, giving the remainder the sign of the divisor. C returns −1 because it uses truncated division, giving the remainder the sign of the dividend.

Conclusion

So the two answers aren’t a contradiction. They’re two rounding rules, each honest about the division identity. Round the quotient down and the remainder takes the divisor’s sign, which is why −7 mod 3 is 2. Round toward zero and it takes the dividend’s sign, which is why −7 mod 3 is −1.

Math prefers the non-negative 2. C, JavaScript and Java hand back −1. Rather than trust whichever default you land on, force the answer with ((a % b) + b) % b or a built-in like Math.floorMod, and the payoff shows up on its own: fewer off-by-one bugs, indices that wrap correctly, and proofs that assume the convention you meant. Drop any negative dividend and divisor into the Remainder Calculator to see the non-negative remainder for the pair.

← Back to Blog