Handle negative modulo correctly across truncated, floored, and Euclidean conventions. Language comparison table, number-line visual, and range-scan highlighting where results differ.
The **Modulo of Negative Numbers Calculator** resolves one of the most confusing topics in modular arithmetic: what happens when the dividend or divisor is negative? Different programming languages and mathematical traditions answer this question differently, leading to subtle bugs when porting code between Python, JavaScript, C, Java, and others.
This calculator evaluates three conventions simultaneously. **Truncated division** (used by JavaScript, C, Java, Go, Rust) rounds the quotient toward zero, so the remainder carries the sign of the dividend. **Floored division** (used by Python, Ruby, Excel) rounds the quotient toward negative infinity, so the remainder carries the sign of the divisor. **Euclidean division** always produces a non-negative remainder in [0, |m|), which is the convention preferred in pure mathematics and number theory.
All three results are displayed side by side, mapped onto a visual number line, and cross-referenced against a table of 10 popular programming languages. A range scanner then evaluates every integer in a configurable window, highlighting rows where the conventions disagree. This makes it easy to see the pattern: the results differ exactly when the dividend is negative (for positive m) or when the divisor is negative, and the Euclidean form always stays non-negative.
Negative-modulo bugs are one of the most frequent sources of off-by-one and sign errors in software. A developer moving from Python to JavaScript may expect `-13 % 5` to return 2 (Python's answer) but gets -3 instead (JavaScript's answer). This tool shows both answers, explains why they differ, and lists exactly which languages use which convention.
For students, seeing the three conventions on the same number line and in the same range table turns an abstract rule into something visual and concrete. The language reference table also serves as a quick cheat-sheet during exams or interviews, and the range scan makes it easier to spot exactly where the conventions begin to diverge.
Truncated: r = a − m × trunc(a/m). Floored: r = a − m × floor(a/m). Euclidean: r = a − |m| × floor(a/|m|), adjusted to [0, |m|).
Result: Truncated: −3, Floored: 2, Euclidean: 2
trunc(−13/5) = −2, so truncated remainder = −13 − 5×(−2) = −3. floor(−13/5) = −3, so floored remainder = −13 − 5×(−3) = 2. Euclidean always returns the non-negative representative.
The core difference is how the quotient is rounded when it is not an integer. **Truncated** rounds toward zero: trunc(−13/5) = −2. **Floored** rounds toward −∞: floor(−13/5) = −3. The remainder is then a − m × q, which flips sign depending on which q you chose. The **Euclidean** convention forces the remainder into [0, |m|) by always rounding the quotient downward relative to |m|. That gives the cleanest mathematical behavior at the cost of a conditional adjustment.
Early mainframes had hardware division that rounded toward zero, so C inherited truncated semantics. Python's designer chose floored division because it keeps the useful invariant that the remainder always matches the sign of the divisor, which simplifies clock-arithmetic and array-indexing patterns. Neither choice is wrong — the mismatch just means developers moving between ecosystems must be aware of the difference.
To convert truncated (T) to floored (F): if T < 0 and m > 0, add m. To convert either to Euclidean: take ((T % m) + |m|) % |m|. Memorizing these one-liners eliminates an entire class of negative-modulo bugs and is far more reliable than trying to reason through signs on the fly.
Python uses floored division (result = 2), while JavaScript uses truncated division (result = −3). Both are valid; they just define "mod" differently.
All three are mathematically valid. Number theory prefers the Euclidean definition (always non-negative). Languages choose based on hardware and historical convention.
Use ((a % m) + m) % m to convert JavaScript's truncated remainder into a non-negative floored remainder. That pattern wraps any negative remainder back into the standard 0-to-m-1 range when m is positive.
Yes. With truncated division, the remainder has the sign of the dividend. With floored division, it has the sign of the divisor. With Euclidean, it is always non-negative.
When a ≥ 0 and m > 0, all three conventions produce the same result. In that case, the sign rules never have a chance to diverge.
On modern hardware, truncated division maps directly to the CPU instruction, making it slightly faster. Floored and Euclidean require an extra adjustment step, but the difference is usually negligible outside tight loops.