Is -7 mod 3 equal to 2 or -1? Both answers are correct - it depends on the convention. This calculator puts floored modulo and truncated remainder side by side so the negative-number behavior is visible at a glance; a modulus of 0 errors immediately, and values past 2^53-1 get a precision warning. A congruence, distributive-law and clock-arithmetic cheat sheet sits below for coding and problem sets.
-7 / 3 = -2.333..., floored quotient -3 remainder 2; truncated quotient -2 remainder -1.
Both numbers are computed in this page's memory only - nothing is uploaded and there is no account. The recent-calculation list stays in this browser; switching devices or clearing site data loses it. Clear my data in the footer removes it.
| Input | Dividend a and modulus b: integers or decimals (scientific notation like 1e9 works); negatives supported; ?a=-7&b=3 deep-links |
|---|---|
| Output | Two results side by side: floored modulo and truncated remainder, each with quotient and decomposition; a number-line block visualization |
| Key parameters | No options; history capped at 12 entries; decimal inputs get an extra note (modular arithmetic is usually defined on integers) |
| Processing | Computed in your browser; inputs never stored or uploaded (history stays in local localStorage) |
| Limits | |a|, |b| up to 1e15; beyond Number.MAX_SAFE_INTEGER (9007199254740991) a precision warning appears |
| Known limits | b = 0 errors (undefined); integers past 2^53 may lose precision (computed anyway, with a warning); real-number mod only, no complex or polynomial modulo |
Both results satisfy "dividend = quotient x modulus + remainder"; the difference is how the quotient is taken: truncated remainder cuts the quotient toward zero, floored modulo rounds it toward negative infinity. They always agree on positive numbers and diverge the moment a negative shows up - which is why the same (-7) % 3 gives 2 in Python and -1 in JavaScript.
| Expression | Modulo (floored) | Remainder (truncated) | Language family |
|---|---|---|---|
| -7 mod 3 | 2 | -1 | Modulo: Python, Ruby, Lua 5.3+ Remainder: C, C++, Java, JavaScript, Go, Rust ( %), Haskell (rem) |
| 7 mod -3 | -2 | 1 | |
| -7 mod -3 | -1 | -1 | |
| 7 mod 3 | 1 | 1 | |
| 7 mod 0 | Error: undefined | ||
| Congruence | a = b (mod n) if and only if n divides (a - b). E.g. 17 = 5 (mod 12) - both sit at the 5 o'clock position. |
|---|---|
| Addition | (a + b) mod n = ((a mod n) + (b mod n)) mod n. Split big numbers before adding to avoid overflow. |
| Multiplication | (a x b) mod n = ((a mod n) x (b mod n)) mod n. Fast exponentiation and RSA both lean on this one. |
| Identity | a mod a = 0; a mod 1 = 0; a mod n < |n| (with floored modulo and positive n, 0 <= remainder < n). |
| Fold negative | ((a % n) + n) % n folds any language's remainder into [0, n) - one line to fix C/JS negative remainders. |
Ring buffers, hash bucketing, periodic scheduling (cron day-of-week): a negative remainder sends your index out of bounds - array index -1 is undefined in JS, not the last element. Computing (a^b) mod p in contests, one unfixed negative in the intermediate steps leaves the answer off by exactly p. I learned this the hard way on a LeetCode problem; my ring indices have been ((i % n) + n) % n ever since.
For positive numbers they agree; the difference shows with negatives. Truncated remainder rounds the quotient toward zero: -7 divided by 3 gives quotient -2, remainder -1. Floored modulo rounds toward negative infinity (or picks a non-negative remainder): -7 mod 3 = 2. Math textbooks and number theory use modulo; C, Java and JavaScript % give the remainder, while Python % gives the modulo.
Modulo requires the remainder to share the sign of the modulus (a positive modulus means a non-negative remainder): -7 = (-3) x 3 + 2, quotient -3 rounded toward negative infinity, remainder 2 inside [0,3). Under truncation, -7 = (-2) x 3 + (-1), quotient -2 rounded toward zero, remainder -1 sharing the dividend's sign. Both are self-consistent - they are just different conventions.
You get an error. Dividing by zero is undefined and so is mod 0; the page tells you the modulus cannot be 0. That is not a crash - it is math refusing to cooperate.
No. The computation happens in this device's browser - there is no upload endpoint and the page works offline. Recent history lives in this browser's localStorage; Clear my data in the footer removes it in one click.