Audited 27 Jul 2026·Last updated 27 Jul 2026·4 citations·Tier 3·0 uses

Modulo Calculator

Free modulo calculator for a mod n with negative operands. Shows BOTH the mathematical (floored) mod and the programming (JS-style) remainder, side by side.

Modulo Calculator

Any integer, positive or negative.
Any nonzero integer, positive or negative.
Mathematical mod (a mod n)
2
a − n·⌊a/n⌋ — the floored-division convention used throughout mathematics. Always shares the sign of n (or is 0).
Programming remainder (a % n)
-1
Floored quotient ⌊a/n⌋
-3
Truncated quotient
-2
Do both conventions agree? (1=yes, 0=no)
0

Background.

The Quanta modulo calculator computes a mod n and shows two different, both mathematically valid, answers whenever a or n is negative — the mathematical (floored) mod and the programming-language (truncated) remainder — because most calculators and most programming languages quietly pick just one convention and never mention the other exists. Enter the defaults, a = −7 and n = 3, and the calculator returns a mathematical mod of 2 alongside a JavaScript-style remainder of −1: both are 'a mod 3' by a defensible definition, and which one you need depends entirely on what you are using the result for.

The disagreement only ever shows up when the dividend and divisor have opposite signs. For all-positive inputs, every convention agrees, which is exactly why this gap goes unnoticed until someone hits a negative case in production code, a modular-arithmetic homework problem involving a negative dividend, or a scheduling/wraparound calculation (day-of-week arithmetic, circular buffer indexing) that happens to need to go 'backward' past zero. Getting the wrong convention silently produces an off-by-one-cycle bug that is notoriously hard to spot, because the two answers are frequently close in magnitude and both look plausible on their own.

What is modulo calculator?

Mathematically, a mod n is defined by the floored-division convention: a mod n = a − n·⌊a/n⌋, where ⌊⌋ is the floor function (round toward negative infinity). This guarantees the result always shares the sign of n (or is exactly 0) and satisfies 0 ≤ result < n for a positive n — the convention used throughout number theory, textbooks, and Knuth's The Art of Computer Programming (Knuth, 1997, Vol. 1, §1.2.4). JavaScript's `%` operator, and the equivalent operator in C, Java, and most C-family languages, instead computes a truncated-division remainder: a − n·trunc(a/n), where trunc rounds toward zero rather than toward negative infinity (ECMA-262, §6.1.6.1.6). This result shares the sign of the dividend a (or is 0), not the sign of n. The two conventions are identical whenever a and n share the same sign, or whenever a is an exact multiple of n — the disagreement appears only when the signs differ and a is not an exact multiple, which is exactly the case this calculator is built to make visible rather than silently pick a side on.

How to use this calculator.

  1. Enter the dividend a — any positive or negative integer.
  2. Enter the divisor n — any nonzero positive or negative integer.
  3. Read flooredMod for the mathematical convention (used in number theory, modular arithmetic proofs, and languages like Python whose `%` also follows this convention) and jsRemainder for what JavaScript's own `%` operator would return on the identical inputs.
  4. Check signsMatch: if it's 1, both conventions agree and you don't need to worry about which one you're using. If it's 0, the choice matters — pick flooredMod for wraparound/cyclic logic (day-of-week, angle normalization, array indexing that should never go negative) and jsRemainder if you specifically need to replicate what a `%` operator in your code would output.

The formula.

a mod n = a − n·⌊a⁄n⌋

Both outputs share the same skeleton, a − n·q, and differ only in how the quotient q is rounded. The mathematical mod uses q = ⌊a/n⌋, the floor (round toward negative infinity), which is why the result always lands with the same sign as n (Knuth, 1997, §1.2.4). The JS-style remainder uses q = trunc(a/n), rounding toward zero instead, which is why its result always shares the sign of a (ECMA-262, §6.1.6.1.6). For a=−7, n=3: a/n = −2.333…, so ⌊−2.333⌋ = −3 but trunc(−2.333) = −2 — two different quotients, one integer apart, and that single-integer gap in the quotient is exactly what produces the different final answers (2 versus −1). Whenever a/n is already an exact integer, or whenever a and n share a sign (so a/n is positive), floor and trunc agree and both conventions collapse to the same number.

A worked example.

Example

Take a = −7 and n = 3, the exact case the brief for this page is built around. −7 ÷ 3 = −2.333…. The floored quotient (round toward negative infinity) is −3, giving a mathematical mod of −7 − 3×(−3) = −7 + 9 = 2. The truncated quotient (round toward zero) is −2, giving a JavaScript-style remainder of −7 − 3×(−2) = −7 + 6 = −1 — matching exactly what `-7 % 3` returns in a JavaScript console. Both 2 and −1 are 'a mod 3' by a defensible, widely-used definition; they are simply different definitions, and this calculator's whole purpose is to show you both rather than silently pick one.

a-7
n3

Frequently asked questions.

Why does -7 % 3 give -1 in JavaScript instead of 2?
Because JavaScript's `%` operator implements a truncated-division remainder, not the mathematical floored mod. It rounds the quotient −7/3 = −2.333… toward zero (giving −2), then computes −7 − 3×(−2) = −1. The mathematical convention instead rounds that same quotient toward negative infinity (giving −3), producing −7 − 3×(−3) = 2. Both are legitimate definitions of 'mod' — JavaScript, C, C++, Java, and C# all use the truncated convention for their `%` operator, while Python's `%` (notably) uses the floored convention, matching the mathematical definition. This is a real, well-documented source of cross-language bugs (Boute, 1992).
Which convention should I use for day-of-week or wraparound calculations?
Use the floored (mathematical) convention almost always. If today is day 0 (say, Sunday) and you want to find what day it was 10 days ago, you need (0 − 10) mod 7, and you want an answer in the range [0, 6] — a valid day index — not a negative number. The floored mod gives exactly that: (−10) mod 7 = 4. The truncated (JS `%`) convention gives −10 % 7 = −3, a negative array index that will silently do the wrong thing if you use it to index into a 7-element day-name array. This is the single most common practical reason to reach for the floored convention over a raw `%` in code that has to go 'backward' past zero.
Do flooredMod and jsRemainder ever give different signs but the same magnitude?
It depends on the specific numbers, not a general rule — the difference between the two results is always exactly n (or −n), since they come from quotients exactly 1 apart. For a=−7, n=3, the two results are 2 and −1, a difference of exactly 3 = n. For a=7, n=−3, the two results are −2 and 1, again a difference of exactly 3 in magnitude (|n|=3). Whenever the two conventions disagree, they will always differ by exactly |n|, not by some unrelated amount — a useful sanity check if you're debugging a modulo-related discrepancy in your own code.

References& sources.

  1. [1]Knuth, D.E. (1997). The Art of Computer Programming, Vol. 1: Fundamental Algorithms, 3rd ed. Addison-Wesley. §1.2.4 "Floor and ceiling; mod" — defines x mod y = x − y⌊x/y⌋.
  2. [2]Boute, R.T. (1992). "The Euclidean Definition of the Functions Div and Mod." ACM Transactions on Programming Languages and Systems, 14(2), 127–144.
  3. [3]ECMA-262, ECMAScript Language Specification — §6.1.6.1.6 "Number::remainder", defining JavaScript's `%` operator (truncated-division remainder convention).
  4. [4]NIST Digital Library of Mathematical Functions, Chapter 27 "Functions of Number Theory".

In this category

Embed

Quanta Pro

Paid features are coming later.

  • All 313 calculators remain free
  • No billing is enabled
Coming soon