Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


Modulo of Negative Numbers






When the Dividend Goes Below Zero

Modulo with positive numbers is unambiguous — divide, take what's left over. But the moment the dividend turns negative, the operation splits into two competing definitions that produce different answers to the same question. The mathematics is consistent either way; the confusion arises from the fact that different contexts — and different programming languages — choose different sides.



The Problem

What is (7)mod3(-7) \bmod 3?

For positive dividends the answer would be immediate. But 7-7 is not a positive number, and "the remainder when 7-7 is divided by 33" depends on what "remainder" means when the dividend is negative.

One answer is 1-1. Another is 22. Both satisfy the division equation a=nq+ra = n \cdot q + r — they simply use different values of the quotient qq, which forces different values of the remainder rr.

This is not a mathematical ambiguity in the sense that either answer is wrong. It is a definitional fork: two conventions exist, each internally consistent, each producing valid results. The trouble begins when the two are mixed — a formula derived under one convention, implemented in a language that uses the other.

Two Conventions

Both conventions start from the same equation:

a=nq+ra = n \cdot q + r


They agree that aa and nn are given and that qq and rr must be determined. They disagree on how to choose qq — and that single choice determines everything.

Truncated division rounds the quotient toward zero. The remainder inherits the sign of the dividend, and can be negative.

Floored division rounds the quotient toward -\infty. The remainder is always non-negative, confined to {0,1,2,,n1}\{0, 1, 2, \ldots, n-1\}.

For positive dividends, both conventions round the same direction and produce identical results. The split only appears when aa is negative — when "toward zero" and "toward -\infty" point in different directions.

Truncated Division (Round Toward Zero)

Under truncated division, the quotient is obtained by dividing and discarding the fractional part — rounding toward zero. The remainder takes whatever sign is needed to satisfy a=nq+ra = n \cdot q + r.

For (7)mod3(-7) \bmod 3: divide 7÷3=2.33-7 \div 3 = -2.33\ldots. Truncate toward zero to get q=2q = -2. Compute the remainder: r=7(3×2)=7+6=1r = -7 - (3 \times -2) = -7 + 6 = -1. The result is 1-1.

For (23)mod5(-23) \bmod 5: divide 23÷5=4.6-23 \div 5 = -4.6. Truncate to q=4q = -4. Remainder: r=23(5×4)=23+20=3r = -23 - (5 \times -4) = -23 + 20 = -3. The result is 3-3.

The remainder under this convention shares the sign of the dividend. A negative dividend always produces a non-positive remainder (r0r \leq 0). This behavior matches how hardware division works on most processors, which is why many compiled languages adopt it.

Floored Division (Round Toward -\infty)

Under floored division, the quotient is rounded down — toward negative infinity, not toward zero. This guarantees a non-negative remainder.

For (7)mod3(-7) \bmod 3: divide 7÷3=2.33-7 \div 3 = -2.33\ldots. Floor to q=3q = -3. Compute the remainder: r=7(3×3)=7+9=2r = -7 - (3 \times -3) = -7 + 9 = 2. The result is 22.

For (23)mod5(-23) \bmod 5: divide 23÷5=4.6-23 \div 5 = -4.6. Floor to q=5q = -5. Remainder: r=23(5×5)=23+25=2r = -23 - (5 \times -5) = -23 + 25 = 2. The result is 22.

The remainder under this convention always falls in {0,1,2,,n1}\{0, 1, 2, \ldots, n-1\}, regardless of the dividend's sign. This is the convention preferred in mathematical texts, because it keeps remainders in the same canonical set used to define congruence classes.

Which Convention Is Correct?

Both are correct. They are different definitions, not different attempts at the same definition.

Mathematics typically prefers floored division. Remainders stay in {0,1,,n1}\{0, 1, \ldots, n-1\}, congruence classes have a single canonical representative per class, and theoretical arguments are cleaner when remainders are non-negative.

Computing typically prefers truncated division. It maps directly to hardware division instructions, requires no sign adjustment, and runs faster. When performance matters and negative inputs are rare or handled separately, the truncated convention is pragmatic.

The choice has no effect on congruence. Under both conventions, 7-7 and 22 are congruent modulo 33: their difference is 9=3×39 = 3 \times 3, a multiple of the modulus. The conventions disagree on which representative to return, not on the underlying mathematical relationship.

Converting Between Conventions

A truncated remainder that comes out negative can be converted to its floored equivalent by adding the modulus.

If truncated division gives (7)mod3=1(-7) \bmod 3 = -1, add 33: 1+3=2-1 + 3 = 2. That is the floored result.

If truncated division gives (23)mod5=3(-23) \bmod 5 = -3, add 55: 3+5=2-3 + 5 = 2. That is the floored result.

The addition is only needed when the remainder is negative. For positive dividends, both conventions agree and no conversion is required.

A universal formula handles both cases automatically:

floored remainder=((a%  n)+n)%  n\text{floored remainder} = ((a \% \; n) + n) \% \; n


This works in any language regardless of which convention its %\% operator uses. If the initial remainder is already non-negative, adding nn and reducing again leaves it unchanged. If it is negative, the addition shifts it into the correct range.

Congruence Perspective

The two conventions produce different numbers, but those numbers represent the same congruence class. From the perspective of congruence, there is no disagreement.

The values 1-1 and 22 are both valid answers to (7)mod3(-7) \bmod 3 because 12(mod3)-1 \equiv 2 \pmod{3}. Their difference is 33, a multiple of the modulus.

Every congruence class modulo nn contains infinitely many integers: ,7,4,1,2,5,8,11,\ldots, -7, -4, -1, 2, 5, 8, 11, \ldots all belong to the same class modulo 33. The truncated convention picks 1-1 as the representative; the floored convention picks 22. Both are members of the same family.

When working with congruences rather than computing specific remainders, the distinction between conventions dissolves entirely. The statement 72(mod3)-7 \equiv 2 \pmod{3} is true regardless of which convention is in play.

Negative Divisor

The discussion so far has assumed a positive modulus n>0n > 0. When the divisor is negative, behavior varies even more unpredictably across definitions and languages.

Mathematical convention typically requires n>0n > 0 and does not define modulo for negative divisors. The remainder is always taken with respect to a positive modulus.

Programming languages that allow negative divisors produce results that vary by language, by convention, and sometimes by version. C and Java allow them but offer few guarantees about the sign of the result. Python handles them consistently under floored division but the behavior is unintuitive.

The safest practice is to avoid negative divisors entirely. Use the absolute value of the modulus and handle any sign logic separately. This eliminates an entire category of edge cases and makes the code's intent explicit.

Common Pitfalls

The most frequent mistake is assuming that %\% behaves the same in every language. Code written in Python, where (7)%  3=2(-7) \% \; 3 = 2, will not produce the same result in Java, where (7)%  3=1(-7) \% \; 3 = -1. Porting algorithms without accounting for this discrepancy introduces subtle errors that pass positive-input tests but fail on negative inputs.

Array indexing is a common casualty. Accessing an array at position (1)%  n(-1) \% \; n is intended to wrap around to the last element — and it does in Python, where the result is n1n - 1. In C or JavaScript, the result is 1-1, which is an invalid index.

Forgetting to test negative inputs is the root cause. Modular code that works perfectly for positive numbers may silently break for negative ones. Any function that computes remainders should be tested with negative dividends explicitly.

Mixing conventions within a single computation — reducing one value under floored division and another under truncated — produces results that belong to no consistent system. Pick one convention and apply it uniformly.

Worked Examples

Compute (10)mod4(-10) \bmod 4 under both conventions. Truncated: 10÷4=2.5-10 \div 4 = -2.5, truncate to 2-2, remainder 10(4×2)=10+8=2-10 - (4 \times -2) = -10 + 8 = -2. Floored: floor to 3-3, remainder 10(4×3)=10+12=2-10 - (4 \times -3) = -10 + 12 = 2.

Compute (1)mod7(-1) \bmod 7 under both conventions. Truncated: 1÷7=0.14-1 \div 7 = -0.14\ldots, truncate to 00, remainder 1(7×0)=1-1 - (7 \times 0) = -1. Floored: floor to 1-1, remainder 1(7×1)=1+7=6-1 - (7 \times -1) = -1 + 7 = 6.

Convert: if a language returns (15)%  6=3(-15) \% \; 6 = -3, the non-negative equivalent is 3+6=3-3 + 6 = 3.

Determine what Python returns for (23)%  5(-23) \% \; 5. Python uses floored division, so: floor 23÷5=4.6-23 \div 5 = -4.6 to 5-5, remainder 23+25=2-23 + 25 = 2. Python returns 22.

Determine what JavaScript returns for the same expression. JavaScript uses truncated division: truncate 4.6-4.6 to 4-4, remainder 23+20=3-23 + 20 = -3. JavaScript returns 3-3.

Ensure non-negative results in JavaScript: ((23%  5)+5)%  5=(3+5)%  5=2%  5=2((-23 \% \; 5) + 5) \% \; 5 = (-3 + 5) \% \; 5 = 2 \% \; 5 = 2.