What is mod? The remainder, defined properly

Mod is the remainder left after division: 17 mod 5 = 2, because 17 = 5 × 3 + 2. Every mod calculation is a division written in the form a = n × q + r, where a is the number you start with (the dividend), n is the modulus (the divisor), q is the quotient and r is the result of a mod n. A mod calculator — also searched for as a modulo calculator or a remainder calculator; all three names describe the same operation — finds q and r for you and shows the equation so the answer can be checked, not just trusted. The only hard rule in the definition is that n must not be zero: with n = 0, every product n × q is 0, so no quotient and remainder can rebuild a, and mod by zero is simply undefined.

For positive numbers, that definition is the whole story, and every textbook, calculator and programming language on Earth agrees: 100 mod 7 = 2 because 7 × 14 = 98 and 100 − 98 = 2. The interesting part — and the reason this page exists in its particular form — is what happens when a sign enters the picture. The equation a = n × q + r does not, by itself, pin down a unique answer for negative inputs, because more than one pair (q, r) can satisfy it. For −17 and 5, both −17 = 5 × (−4) + 3 and −17 = 5 × (−3) + (−2) are perfectly true equations. Choosing between them requires an extra rule about how q is rounded, and mathematics and most programming languages chose different rules. Sections below give both rules in full; the calculator above always shows both answers, labelled, so the choice is never hidden from you.

How to calculate mod, step by step

Quick answer: To calculate a mod n by hand, find the largest multiple of n that does not pass a (for positive numbers), subtract it, and the leftover is the answer. 100 mod 7: the largest multiple of 7 not passing 100 is 98, and 100 − 98 = 2.

Step 1 — divide. Work out a ÷ n as an ordinary division: 100 ÷ 7 = 14.2857… Step 2 — take the whole part of the quotient. For positive numbers, discard the fraction: q = 14. Step 3 — multiply back. n × q = 7 × 14 = 98. Step 4 — subtract. r = a − n × q = 100 − 98 = 2. Step 5 — check. Rebuild the equation: 7 × 14 + 2 = 100. ✓ If the check fails, the mod is wrong — this five-second test catches almost every slip, which is why the calculator prints the full equation for both conventions with every result. The same steps answer the everyday question how to calculate mod on paper, on a calculator without a mod button (divide, drop the fraction, multiply back, subtract), and in code — the only place the recipe needs care is Step 2 when negative numbers are involved, because "take the whole part" can mean rounding down or rounding toward zero, and those differ by exactly one for negative non-multiples.

One more hand-calculation worth knowing covers the negative case directly, since it is the version examinations actually set. To find the mathematical value of −17 mod 5 without a calculator, ask: what multiple of 5 sits just below −17 on the number line? The multiples near it are −15 and −20; the one below is −20, which is 5 × (−4). Subtract: −17 − (−20) = 3. That is the whole floored method in one picture — walk down to the first multiple at or below your number, then count up the gap. The truncated method, by contrast, walks toward zero to −15 and counts a gap of −2. Same number line, same multiples, two different walking directions: that picture is the entire floored-versus-truncated distinction, and every disagreement between textbooks and programming languages reduces to it.

The two conventions: floored modulo vs truncated remainder

The mathematical (floored) convention rounds the quotient down — toward negative infinity — so the remainder always has the same sign as the divisor n, or is zero. With the usual positive modulus, that means the floored modulo always lands in the tidy range 0 to n − 1: −17 mod 5 = 3, because the floored quotient is −4 (floor of −3.4) and −17 = 5 × (−4) + 3. This is the convention of number theory, of modular arithmetic as taught in mathematics courses, of Python, and of spreadsheet MOD functions. Its great virtue is consistency: adding or subtracting whole multiples of n never changes the result's range, which is exactly what clock arithmetic, calendars and congruence classes need.

The truncated convention rounds the quotient toward zero, so the remainder always has the same sign as the dividend a, or is zero: −17 % 5 = −2, because the truncated quotient is −3 and −17 = 5 × (−3) + (−2). This is the convention built into the % operator of JavaScript, C, C++, Java, C# and Rust, and into the hardware divide instructions those languages historically mirrored. The MDN documentation for JavaScript is admirably blunt about it: % is a remainder operator, not a true modulo, and its result takes the sign of the dividend. Neither convention is a bug. They are two different, precisely defined operations that happen to share a name and a symbol — and to agree whenever the signs make the two rounding rules coincide, which includes all positive inputs and, less obviously, cases like −17 mod −5 where both give −2.

Which convention should I use?
Use the floored (mathematical) result for mathematics homework, exams, modular arithmetic, clocks, calendars and anything phrased as "mod". Use the truncated result when you are matching what JavaScript, C, Java or similar code will actually compute with %. If a specification just says "mod" about negative numbers without defining it, that specification is ambiguous — quote both values and the convention you chose.

Mod of negative numbers, worked in full

The mod of negative numbers is where most online calculators quietly pick a side without telling you. Here are the three instructive cases, each verified against the check equation. Case 1: −17 mod 5. Floored: the quotient must round down from −3.4 to −4, giving r = −17 − 5 × (−4) = 3. Truncated: the quotient rounds toward zero to −3, giving r = −17 − 5 × (−3) = −2. The answers differ by exactly n — they always do when the conventions split — because the quotients differ by exactly 1. Case 2: 17 mod −5. A negative modulus is rare in school mathematics but perfectly defined. Floored: 17 ÷ (−5) = −3.4 rounds down to −4, so r = 17 − (−5) × (−4) = 17 − 20 = −3, which correctly carries the divisor's (negative) sign. Truncated: the quotient rounds toward zero to −3, so r = 17 − (−5) × (−3) = 17 − 15 = 2, carrying the dividend's (positive) sign. Case 3: −17 mod −5. Here the conventions agree: both quotients are 3 (floor of 3.4, and truncation of 3.4, are both 3), so both remainders are −17 − (−5 × 3) = −2. A useful mental shortcut for the common textbook case — negative dividend, positive modulus — is: take the truncated remainder and, if it is negative, add n once. −2 + 5 = 3, the floored answer. That single adjustment converts any JavaScript or C % result into the mathematical modulo whenever n is positive.

Worked examples table

Every row below was produced by the calculator above and checked against the equation a = n × q + r for both conventions, so you can reproduce each one exactly by typing the same numbers in.

anFloored qFloored r (math mod)Truncated qTruncated r (JS %)
1753232
-175-43-3-2
17-5-4-3-32
-17-53-23-2
1007142142
123456789123456789971272750403334606712727504033346067

Read the table by sign pattern. Positive ÷ positive: always agreement. Negative ÷ positive: the classic split — floored stays in 0…n−1, truncated goes negative. Positive ÷ negative: split again, mirrored. Negative ÷ negative: agreement, because floor and truncation round the (positive) quotient the same way. Knowing which pattern you are in tells you in advance whether the convention question even matters for your numbers.

What each programming language actually does

JavaScript: % is truncated remainder; −17 % 5 is −2, and x % 0 is NaN. There is no built-in floored mod; the standard idiom is ((a % n) + n) % n for positive n. Python: % is floored; −17 % 5 is 3, matching mathematics, and divmod returns the floored pair. C and C++: % is truncated (guaranteed since C99 / C++11); earlier C standards left the direction implementation-defined, a historical source of porting bugs. Java and C#: % is truncated; Java additionally offers Math.floorMod, which is floored — the existence of a separately named method is an admission that the two operations are different. Rust: % is truncated, with rem_euclid providing the floored-style Euclidean variant. Spreadsheets: Excel and Google Sheets MOD is floored, so MOD(-17, 5) returns 3 — meaning a spreadsheet and a JavaScript program given "the same" formula can legitimately produce different answers. The lesson is not that one side is sloppy; it is that "mod" names a family of two operations, and any serious tool — including this page — should say which one it computed. That is why both results here carry their convention in the label, every time.

Where mod is used: clocks, check digits and hashing

Clocks and calendars are floored mod in daily dress: hours are mod 12 or 24, weekdays mod 7, minutes mod 60. Nine o'clock plus seven hours is (9 + 7) mod 12 = 4 o'clock precisely because whole cycles are discarded and only the position inside the cycle survives — and floored mod is what makes "three hours before midnight" land on 9 rather than on a negative hour. Check digits — the last digit of a barcode, ISBN or card number — are computed with mod 10 or mod 11 (or mod 97 for IBAN bank numbers) so that a mistyped digit changes the remainder and exposes the error. Hashing and pseudorandom generation use mod to fold arbitrarily large values into a fixed table size or range: hash mod table-size picks the bucket. Cryptography is built on modular arithmetic at scales where the numbers have hundreds of digits, which is one reason exact big-integer mod — the subject of the next section — matters in practice and not just in puzzles. Even the humble "is it even?" test is mod: a mod 2 = 0 exactly when a is even.

A concrete check-digit example ties several of these uses together. An ISBN-13 is validated by multiplying its digits alternately by 1 and 3, adding the products, and taking the total mod 10: the check digit is exactly the value that makes the grand total congruent to 0 modulo 10. Change any single digit and the total's remainder changes, so the validation fails — the same remainder logic as 100 mod 7, put to work catching typos in every bookshop on Earth. IBAN bank account numbers use mod 97 for the same reason, which is why the BigInt example on this page uses 97 as its modulus: remainders modulo 97 are a real banking mechanism, not a classroom curiosity.

Very large integers and why BigInt matters

Quick answer: Ordinary calculators store numbers as floating point and silently lose exactness beyond 2^53 = 9007199254740992. This calculator uses BigInt — exact arbitrary-precision integers — so 123456789123456789 mod 97 comes out as exactly 7, and the check equation rebuilds the input digit for digit.

Try the big row of the examples table in a basic calculator and watch what happens: 123456789123456789 cannot even be stored exactly as a floating-point number — it is rounded to a nearby multiple of 16 before any division happens — so the "remainder" such a calculator reports is the remainder of a different number. BigInt arithmetic has no such ceiling: integers are stored digit by digit and divided exactly, at any length, which is why this page imposes no size limit at all. The trade-off is speed on truly enormous inputs, but for anything a person would type or paste, the calculation is instant. The check equation is your proof: 97 × 1272750403334606 + 7 = 123456789123456789, exactly, with no rounding anywhere in the chain. Whenever a tool gives you a remainder for a large number without showing a check that rebuilds the input, treat the result as unverified.

Common mod mistakes

Assuming % in code is mathematical mod. The single most common mod bug in real software: a wraparound index computed as (i − 1) % length in JavaScript goes negative instead of wrapping, because % truncates. The fix is the floored adjustment, ((x % n) + n) % n. Expecting one right answer for negatives without naming a convention. If two sources disagree on −17 mod 5, they are probably both right under different rules — check the quotient each one used. Dividing by zero and accepting a placeholder. Mod 0 is undefined; a tool that returns 0 or the dividend itself for a mod 0 is inventing mathematics. This calculator refuses, plainly. Trusting floating point for big integers. Beyond 2^53, doubles cannot represent every integer, so remainders computed from them can be wrong while looking precise. Forgetting the check. a = n × q + r takes seconds to verify and catches sign errors, rounding errors and typos in one step — the calculator prints it for both conventions so there is no excuse to skip it.

Sources and References

Source-verification note: six of the seven sources above returned HTTP 200 to a direct request during this page's build check. The Art of Problem Solving wiki blocks automated requests (its server closed the connection to our checker), so that source was verified instead by full content retrieval — the complete introduction article was read end to end before it was cited. No source was substituted: all seven are the standard references intended for this topic. Every figure in this guide was calculated with this page's own method and independently checked, so you can reproduce each one exactly by entering the same numbers above.