9314JIUsi
Utilities · Browser-local arithmetic

Modulo Calculator: Settle -7 mod 3 Once and for All

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.

Modulo · Floored (math convention)
2
-7 = (-3) × 3 + 2
Remainder shares the modulus sign; number theory, Python
Remainder · Truncated (C-family convention)
-1
-7 = (-2) × 3 + (-1)
Remainder shares the dividend sign; C, Java, JS %

-7 / 3 = -2.333..., floored quotient -3 remainder 2; truncated quotient -2 remainder -1.

Recent

    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.

    InputDividend a and modulus b: integers or decimals (scientific notation like 1e9 works); negatives supported; ?a=-7&b=3 deep-links
    OutputTwo results side by side: floored modulo and truncated remainder, each with quotient and decomposition; a number-line block visualization
    Key parametersNo options; history capped at 12 entries; decimal inputs get an extra note (modular arithmetic is usually defined on integers)
    ProcessingComputed 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 limitsb = 0 errors (undefined); integers past 2^53 may lose precision (computed anyway, with a warning); real-number mod only, no complex or polynomial modulo

    Modulo vs Remainder: Two Conventions, One Character Apart

    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.

    ExpressionModulo (floored)Remainder (truncated)Language family
    -7 mod 32-1Modulo: Python, Ruby, Lua 5.3+
    Remainder: C, C++, Java, JavaScript, Go, Rust (%), Haskell (rem)
    7 mod -3-21
    -7 mod -3-1-1
    7 mod 311
    7 mod 0Error: undefined

    Modular arithmetic cheat sheet

    Congruencea = 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.
    Identitya 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.

    When the difference bites

    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.

    Related tools

    What is the difference between modulo and remainder?

    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.

    Why does -7 mod 3 equal 2?

    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.

    What happens when the modulus is 0?

    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.

    Are my numbers uploaded anywhere?

    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.