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.

Key Terms

Remainder— the central issue here: its sign depends on the division convention
Congruence— both conventions produce values in the same congruence class
Modulus— the divisor whose sign convention is also discussed
Residue Class— the two conventions select different representatives from the same class

See All Arithmetic Definitions →


The Problem

What is (−7) mod 3(-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=n⋅q+ra = n \cdot q + r — they simply use different values of the quotient qq, which forces different values of the remainder rr.

Pitfall: Ignoring the minus sign
Dropping the sign gives (−7) mod 3=7 mod 3=1(-7) \bmod 3 = 7 \bmod 3 = 1, but 11 fails the division equation: −7−1=−8-7 - 1 = -8 is not a multiple of 33. The answer is −1-1 or 22.

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:

Two Conventions
a=n⋅q+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,…,n−1}\{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.
Number line with multiples of 3: for -7, truncation stops at -6 and leaves -1; flooring goes down to -9 and leaves 2 −12−9−6−3truncated: q = −2, r = −1floored: q = −3, r = 2(−7) mod 3, circles: multiples of 3−7Toward zero gives −1; toward −∞ gives 2
Two ways to divide −7 by 3

The circles mark the multiples of 3 and the amber dot is −7. Truncated division rounds −7 ÷ 3 = −2.33 toward zero, to q = −2, and uses the multiple −6 to the right of −7: the remainder is −7 − (−6) = −1. Floored division rounds down, to q = −3, and uses the multiple −9 to the left: the remainder is −7 − (−9) = 2. Both satisfy −7 = 3q + r.

When the division is not exact, the two remainders differ by exactly n: here 2 − (−1) = 3.
Every convention solves the same equation a=n⋅q+ra = n \cdot q + r; only the rounding of the quotient differs, and the remainder follows from it.

Truncated Division (Round Toward Zero)

Fact: C left the sign to the compiler
Until its 1999 standard, C let each compiler choose, so (−7)%  3(-7) \% \; 3 could legally print −1-1 on one machine and 22 on another. C99 settled on truncation, the rule Fortran already followed.

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=n⋅q+ra = n \cdot q + r.

Example
For (−7) mod 3(-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.

Example
For (−23) mod 5(-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 (r≤0r \leq 0). This behavior matches how hardware division works on most processors, which is why many compiled languages adopt it.
The first example of this section, one step at a time. Press Next to carry out each step.
DownloadPNG2×PDF
(−7) mod 3(-7) \bmod 3Step 1 of 3
Step
(−7) mod 3(-7) \bmod 3
1
Divide
−7÷3=−2.33…-7 \div 3 = -2.33\ldots
2
Discard the fractional part to get the quotientRounding toward zero
q=−2q = -2
3
Compute the remainder r=a−n⋅qr = a - n \cdot q
r=−7−(3×−2)=−7+6=−1r = -7 - (3 \times -2) = -7 + 6 = -1
Truncated division on one numberLearn Math Class

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. Rounding down is the floor function, explained on the integer and fractional part page.

Example
For (−7) mod 3(-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.

Example
For (−23) mod 5(-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.

Pitfall: Flooring toward zero
Taking the floor of −3.4-3.4 as −3-3 in (−17) mod 5(-17) \bmod 5 gives r=−17+15=−2r = -17 + 15 = -2, a negative remainder. The floor is −4-4, the next integer down: r=−17+20=3r = -17 + 20 = 3.

The remainder under this convention always falls in {0,1,2,…,n−1}\{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.
DownloadPNG2×PDF
Layout
AspectTruncated divisionFloored division
Quotient rounded toward…zero (discard fractional part)negative infinity (floor function ⌊·⌋)
Sign of remainder for a < 0shares the sign of a (r ≤ 0)always non-negative (r ≥ 0)
Remainder rangedepends on sign of a: r ∈ (−n, n)always r ∈ {0, 1, …, n − 1}
Worked example: (−7) ÷ 3q = −2, r = −1q = −3, r = 2
Typical contextcomputing — matches hardware DIV instructions, faster, no sign adjustment neededmathematics — canonical representative of every congruence class
Floored Division (Round Toward -\\infty)Learn Math Class

Which Convention Is Correct?

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

Fact: Python floors for times before 1970
Python's creator chose floored division with clocks in mind: a moment tt seconds after the start of 1970 has time of day t mod 86,400t \bmod 86{,}400. One second before 1970, at t=−1t = -1, flooring gives 86,39986{,}399, which is 23:59:59, while truncation gives a meaningless −1-1.

Mathematics typically prefers floored division. Remainders stay in {0,1,…,n−1}\{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) mod 3=−1(-7) \bmod 3 = -1, add 33: −1+3=2-1 + 3 = 2. That is the floored result.

If truncated division gives (−23) mod 5=−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:

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

Pitfall: Dropping the final reduction
Writing (a%  n)+n(a \% \; n) + n without the outer %  n\% \; n breaks non-negative remainders: for a=7a = 7, n=3n = 3 it gives 1+3=41 + 3 = 4, outside {0,1,2}\{0, 1, 2\}. The full formula gives 4%  3=14 \% \; 3 = 1.

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.
The universal formula read for the two signs a truncated remainder can have, one per row. Click a row for what the formula does there.
DownloadPNG2×PDF
Formula
floored remainder=((a%  n)+n)%  n\text{floored remainder} = ((a \% \; n) + n) \% \; n
When the conversion changes the remainderLearn Math Class
Only the sign of the truncated remainder matters: a negative one is shifted up by nn, a non-negative one passes through unchanged.
Whatever convention a language follows, ((a%  n)+n)%  n((a \% \; n) + n) \% \; n always returns the remainder in {0,1,…,n−1}\{0, 1, \ldots, n-1\}.

Congruence Perspective

When the dividend is negative and the division is not exact, 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) mod 3(-7) \bmod 3 because −1≡2(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 −7≡2(mod3)-7 \equiv 2 \pmod{3} is true regardless of which convention is in play.
123456789101112131415161718192021222324252627282930313233343536★ 012345mod 6
One class, many representatives

Every number in a slot is congruent to every other: −1 and 2 occupy the same slot mod 3 just as 3, 9 and 15 share a slot mod 6 here, so the two conventions’ answers, −1 and 2, are two names for one class. The wheel does not care which representative you write. Step backwards through negative numbers on the modular arithmetic wheel.

Choosing a convention is choosing which member of the class to report, nothing more.

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 (since C99) and Java allow them and define the result by truncating the quotient, so the remainder takes the sign of the dividend. 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 n−1n - 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.
DownloadPNG2×PDF
1Ignoring the minus sign
The minus sign of the dividend is set aside, the remainder of the positive number is computed, and that remainder is given as the answer for the negative one.
Wrong turn
Error here
7=3⋅2+17 = 3 \cdot 2 + 1, so (−7) mod 3=1(-7) \bmod 3 = 1.
Right path
The division equation must hold for −7-7 itself. Truncated: −7=3⋅(−2)+(−1)-7 = 3 \cdot (-2) + (-1), so the remainder is −1-1. Floored: −7=3⋅(−3)+2-7 = 3 \cdot (-3) + 2, so the remainder is 22. Neither convention gives 11.
Why it happensIn multiplication and division the size of a number and its sign can be handled separately, and that habit is carried over to remainders, where it does not work.
How to checkSubtract the answer from the dividend; the difference must be a multiple of nn. −7−1=−8-7 - 1 = -8 is not a multiple of 33, while −7−(−1)=−6-7 - (-1) = -6 and −7−2=−9-7 - 2 = -9 are.
2Flooring toward zero
Under floored division, the quotient of a negative division is rounded to the neighbouring integer toward zero, as truncation does, instead of down.
Wrong turn
Error here
−17÷5=−3.4-17 \div 5 = -3.4, floored to −3-3, so r=−17−(5×−3)=−17+15=−2r = -17 - (5 \times -3) = -17 + 15 = -2.
Right path
The floor of −3.4-3.4 is −4-4, the greatest integer less than or equal to −3.4-3.4. Then r=−17−(5×−4)=−17+20=3r = -17 - (5 \times -4) = -17 + 20 = 3, and indeed −17=5⋅(−4)+3-17 = 5 \cdot (-4) + 3.
Why it happensFor positive numbers, flooring and dropping the decimal part give the same integer, so the floor is remembered as "drop the decimals". For a negative number, dropping the decimals moves up, toward zero.
How to checkA floored remainder must lie in {0,1,…,n−1}\{0, 1, \ldots, n-1\}. A negative result such as −2-2 means the quotient was rounded the wrong way.
3Dropping the final reduction
The conversion formula is shortened to (a%  n)+n(a \% \; n) + n. The added nn lifts a negative remainder into range, but it also lifts a remainder that was already non-negative out of range.
Wrong turn
Error here
For n=3n = 3, (7%  3)+3=1+3=4(7 \% \; 3) + 3 = 1 + 3 = 4, and ((−9)%  3)+3=0+3=3((-9) \% \; 3) + 3 = 0 + 3 = 3.
Right path
((7%  3)+3)%  3=4%  3=1((7 \% \; 3) + 3) \% \; 3 = 4 \% \; 3 = 1, and (((−9)%  3)+3)%  3=3%  3=0(((-9) \% \; 3) + 3) \% \; 3 = 3 \% \; 3 = 0.
Why it happensThe formula is written to repair negative remainders and is tried only on inputs such as −7-7, where (−1)+3=2(-1) + 3 = 2 is already correct.
How to checkTest a positive dividend and an exact multiple as well. Every result must lie in {0,1,…,n−1}\{0, 1, \ldots, n-1\}; for n=3n = 3, neither 44 nor 33 does.
Common pitfalls with negative remainders·/arithmetic/modulo/negative-numbersLearn Math Class
Each slip forgets that the division equation must hold for the negative number itself. The minus sign changes the quotient, a floored quotient rounds −3.4-3.4 down to −4-4, and the adjustment (a%  n)+n(a \% \; n) + n needs one more %  n\% \; n to land in range. Subtracting the answer from the dividend and checking for a multiple of nn, or checking that the result lies in {0,1,…,n−1}\{0, 1, \ldots, n-1\}, catches each slip.

Worked Examples

Compute (−10) mod 4(-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) mod 7(-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.

To try these cases with your own numbers, use the modulo calculator.
DownloadPNG2×PDF
Layout
ExpressionTruncated resultFloored result
(−7) mod 3−12
(−10) mod 4−22
(−1) mod 7−16
(−15) mod 6−33
(−23) mod 5−32
Worked ExamplesLearn Math Class

Summary: A Decision Guide for Negative Modulo

The two conventions, the conversion formula, the congruence perspective, the pitfalls — these reduce to a small set of practical decisions a reader actually faces when writing or reading code with negative inputs. The table below collects those situations and the matching action, with a concrete example or note for each.
DownloadPNG2×PDF
Layout
SituationWhat to doExample or note
Need the canonical mathematical remainder (in {0, …, n − 1})apply ((a % n) + n) % n — works in any language regardless of the native conventionJavaScript: (−7) % 3 = −1 → ((−1) + 3) % 3 = 2
Working with congruence relations onlyeither convention is fine — the two results differ by a multiple of n and represent the same class−7 ≡ −1 ≡ 2 (mod 3)
Code may run in multiple languagesapply ((a % n) + n) % n uniformly — the universal idiom is convention-agnosticavoids Python-vs-JavaScript divergence on negative inputs
Indexing into an array with wrap-aroundforce non-negative with ((i % n) + n) % n — never trust raw % for this(−1) % n returns n − 1 in Python (OK) but −1 in JavaScript (invalid index)
Negative divisor (n < 0)avoid — replace n with |n| and handle any sign logic explicitlybehavior is unpredictable across languages and conventions
Summary: A Decision Guide for Negative ModuloLearn Math Class

Skills You Should Learn

Each skill opens its step-by-step method page. Skills marked "On this page" are taught above.
Taught on this page
  • Find the modulo of a negative numberRound the quotient down, not toward zero, so the remainder lands in 0,…,n−10, \ldots, n - 1: (−38) mod 7=4(-38) \bmod 7 = 4 because −38=7⋅(−6)+4-38 = 7 \cdot (-6) + 4.On this page ↑
More arithmetic methods
  • Do modular arithmeticFind the remainder of a sum, product or power: reduce each number mod nn first, operate on the remainders, reduce again; use cycles for powers.
  • Find the GCD with the Euclidean algorithmReplace the pair (a,b)(a, b) by (b,a mod b)(b, a \bmod b) until the remainder is 00; the last nonzero remainder is the GCD. No factoring needed.

Negative Modulo FAQ

What is (-7) mod 3?

+
It depends on the convention. Truncated division (C, Java, JavaScript) rounds the quotient toward zero: -7 = 3 × (-2) - 1, so the answer is -1. Floored division (Python, and standard mathematics) rounds toward negative infinity: -7 = 3 × (-3) + 2, so the answer is 2. Both are valid — they name the same congruence class by different representatives.Read more →

Why does Python give different modulo results than JavaScript?

+
Python uses floored division, so (-7) % 3 = 2; JavaScript uses truncated division, so (-7) % 3 = -1. The two conventions agree for positive operands and split only for negative ones. C and Java side with JavaScript; standard mathematical usage sides with Python. Code ported between languages must be tested with negative dividends explicitly.Read more →

How do you convert a negative remainder to a positive one?

+
Add the modulus. If truncated division gives (-7) mod 3 = -1, add 3 to get 2. The universal formula ((a % n) + n) % n works in any language: the inner operation produces a remainder somewhere in (-n, n), adding n lifts it above zero, and the final reduction brings values that were already non-negative back into range unchanged.Read more →

Why does array indexing fail with negative modulo?

+
Wrapping backward through a circular buffer relies on the remainder staying non-negative. In Python, (-1) % n = n-1, which correctly lands on the last element. In C, Java, or JavaScript, (-1) % n = -1 — an invalid index that throws or reads out of bounds. Code that steps backward must use ((index % n) + n) % n to guarantee a valid position.Read more →

Why did Python choose floored modulo?

+
Because of clocks. Python's creator wanted t mod 86,400t \bmod 86{,}400 to give the time of day for a moment tt seconds after the start of 1970, including moments before it. At t=−1t = -1, floored division gives 86,39986{,}399, which is 23:59:59 on the last day of 1969, while truncation gives −1-1, which is no time of day at all. A floored remainder always lands in {0,1,…,n−1}\{0, 1, \ldots, n - 1\}, a valid position in the cycle.Read more →

Why did old C compilers disagree about (-7) % 3?

+
Because the C standard left the choice open until 1999. Before C99, when an operand was negative, each compiler could decide whether integer division rounded toward zero or down, so (−7)%  3(-7) \% \; 3 could legally print −1-1 on one machine and 22 on another. C99 settled on truncation, the rule Fortran already followed, so modern C always gives −1-1.Read more →

Can you drop the minus sign and take 7 mod 3 instead?

+
No. Dropping the sign gives 7 mod 3=17 \bmod 3 = 1, but −7−1=−8-7 - 1 = -8 is not a multiple of 33, so 11 fails the division equation under both conventions. The answers are −1-1 (truncated) and 22 (floored). The sign can only be carried in front under truncation, which rounds both 73\frac{7}{3} and −73-\frac{7}{3} toward zero: there (−7)%  3=−(7%  3)=−1(-7) \% \; 3 = -(7 \% \; 3) = -1, not +1+1.Read more →