Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools

How to Find the Modulo of a Negative Number

In mathematics the remainder a mod na \bmod n always lies between 00 and n−1n - 1, even when aa is negative. Round the quotient down, toward −∞-\infty, then subtract nn times that quotient from aa.

Recognize the task

A negative dividend: (−7) mod 3(-7) \bmod 3, (−38) mod 7(-38) \bmod 7, −100%  9-100 \% \; 9 in code, or a clock or calendar counted backward. Test: if your remainder came out negative, it used the truncated convention; the mathematical answer is that number plus nn.
Both conventions are compared in the modulo of negative numbers lesson.

Ways to do it

Floored divisionusual
a mod n=a−n⌊an⌋a \bmod n = a - n \left\lfloor \dfrac{a}{n} \right\rfloor
Round the quotient down with the floor (floored division).
Add multiples of nmental
a mod n=a+kn,k chosen so that 0≤a+kn<na \bmod n = a + k n, \quad k \text{ chosen so that } 0 \le a + k n < n
Adding nn does not change the remainder, by congruence; add copies until the result is in range.
Fix a truncated remainderin code
((a%  n)+n)%  n((a \% \; n) + n) \% \; n
Turns the truncated result of C, Java or JavaScript into the mathematical one (converting between conventions).

Steps

StepOn (−38) mod 7(-38) \bmod 7
1Divide and round the quotient down. Down means toward −∞-\infty: −5.43-5.43 rounds down to −6-6, not to −5-5.−38÷7=−5.43…-38 \div 7 = -5.43\ldots, so q=−6q = -6
2Multiply back. This is the largest multiple of nn at or below aa.7⋅(−6)=−427 \cdot (-6) = -42
3Subtract it from the dividend. The gap from that multiple up to aa is the remainder.−38−(−42)=4-38 - (-42) = 4
4Check the range and the division equation. The remainder must satisfy 0≤r<n0 \le r < n and a=n⋅q+ra = n \cdot q + r.−38=7⋅(−6)+4-38 = 7 \cdot (-6) + 4, so (−38) mod 7=4(-38) \bmod 7 = 4

Worked examples

Example 1 · a small negative

floor
−2÷5=−0.4-2 \div 5 = -0.4, so q=−1q = -1
round down, not to 00
−2−5⋅(−1)=−2+5-2 - 5 \cdot (-1) = -2 + 5
subtract n⋅qn \cdot q
(−2) mod 5=3(-2) \bmod 5 = 3
in range 00 to 44

Example 2 · adding multiples

mental
(−47) mod 6(-47) \bmod 6
nearest multiple of 66 above 4747 is 4848
−47+6⋅8=−47+48=1-47 + 6 \cdot 8 = -47 + 48 = 1
add 88 copies of 66
(−47) mod 6=1(-47) \bmod 6 = 1
0≤1<60 \le 1 < 6

Example 3 · fixing a result from code

in code
−100%  9=−1-100 \% \; 9 = -1
JavaScript rounds toward zero
−1+9=8-1 + 9 = 8
add the modulus once
(−100) mod 9=8(-100) \bmod 9 = 8
check: −100=9⋅(−12)+8-100 = 9 \cdot (-12) + 8
Step backward around the circle of remainders in the modular wheel.

See it happen

Picture the multiples of 77 as fence posts along the number line: …,−49,−42,−35,…\ldots, -49, -42, -35, \ldots The remainder is the distance from the nearest post on the left up to the number. For −38-38 that post is −42-42, not −35-35, because −35-35 lies to the right. The distance 44 is positive, and it is always less than the gap 77 between posts.

Common mistakes

Dropping the minus sign

✗ (−38) mod 7=38 mod 7=3(-38) \bmod 7 = 38 \bmod 7 = 3
✓ (−38) mod 7=4(-38) \bmod 7 = 4, since −38=7⋅(−6)+4-38 = 7 \cdot (-6) + 4

Rounding the quotient toward zero

✗ q=−5q = -5:   −38−7⋅(−5)=−3\; -38 - 7 \cdot (-5) = -3
✓ round down to q=−6q = -6: −38+42=4-38 + 42 = 4

Dropping the final reduction in code

✗ (a%  n)+n(a \% \; n) + n for a=14a = 14, n=7n = 7:   0+7=7\; 0 + 7 = 7
✓ ((14%  7)+7)%  7=0((14 \% \; 7) + 7) \% \; 7 = 0