Modulo of Negative Numbers Calculator

Handle negative modulo correctly across truncated, floored, and Euclidean conventions. Language comparison table, number-line visual, and range-scan highlighting where results differ.

Truncated Remainder
-3
-13 โˆ’ 5 ร— trunc(-13/5) = -13 โˆ’ 5 ร— -2
Floored Remainder
2
-13 โˆ’ 5 ร— floor(-13/5) = -13 โˆ’ 5 ร— -3
Euclidean Remainder
2
Always non-negative: result in [0, |5|)
Truncated Quotient
-2
trunc(-13 / 5)
Floored Quotient
-3
floor(-13 / 5)
Conventions Differ?
Yes โ€” be careful!
Truncated (-3) โ‰  Floored (2)

Results on Number Line

T
F
E
T = Truncated (-3) ยท F = Floored (2) ยท E = Euclidean (2)

Language / Convention Reference

LanguageConventionFormulaRemainder SignResult for -13 mod 5
Pythonflooredr = a โˆ’ m ร— floor(a/m)sign of divisor2
JavaScripttruncatedr = a โˆ’ m ร— trunc(a/m)sign of dividend-3
C / C++truncatedr = a โˆ’ m ร— trunc(a/m)sign of dividend-3
Javatruncatedr = a โˆ’ m ร— trunc(a/m)sign of dividend-3
Rubyflooredr = a โˆ’ m ร— floor(a/m)sign of divisor2
Haskell (mod)flooredr = a โˆ’ m ร— floor(a/m)sign of divisor2
Haskell (rem)truncatedr = a โˆ’ m ร— trunc(a/m)sign of dividend-3
Gotruncatedr = a โˆ’ m ร— trunc(a/m)sign of dividend-3
Rusttruncatedr = a โˆ’ m ร— trunc(a/m)sign of dividend-3
Excel MODflooredr = a โˆ’ m ร— floor(a/m)sign of divisor2

Convention Comparison (a from -20 to 20, m = 5)

16 of 41 values differ between conventions. Rows with differences are highlighted.

aTruncatedFlooredEuclidean
-20000
-19-411
-18-322
-17-233
-16-144
-15000
-14-411
-13-322
-12-233
-11-144
-10000
-9-411
-8-322
-7-233
-6-144
-5000
-4-411
-3-322
-2-233
-1-144
0000
1111
2222
3333
4444
5000
6111
7222
8333
9444
10000
11111
12222
13333
14444
15000
16111
17222
18333
19444
20000
Planning notes, formulas, and examples

About the Modulo of Negative Numbers Calculator

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.

When This Page Helps

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 calculator 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.

How to Use the Inputs

  1. Enter the dividend (a) and divisor (m) โ€” either or both can be negative.
  2. Click a preset for common negative-modulo examples.
  3. Compare the three output cards: Truncated, Floored, Euclidean.
  4. Check the language reference table to see which languages match which result.
  5. Adjust the range start / end to scan many a values and spot where conventions diverge.
  6. Look at highlighted rows in the comparison table for quick identification of differences.
Formula used
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|).

Example Calculation

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.

Tips & Best Practices

  • Use the Euclidean result when you need a guaranteed non-negative remainder.
  • In JavaScript, wrap with ((a % m) + m) % m for Python-style behavior.
  • The highlighted rows in the range table show exactly when conventions diverge.
  • Check the language table before porting modular code across languages so you do not mix remainder conventions.
  • Be especially careful with negative dividends โ€” that is where bugs hide.
  • When m is negative, floored and truncated produce the same magnitude but may differ in sign.

The Three Division Conventions Explained

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.

Why Programming Languages Disagree

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.

Practical Conversion Formulas

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.

Sources & Methodology

Last updated:

Frequently Asked Questions

  • Python uses floored division (result = 2), while JavaScript uses truncated division (result = โˆ’3). Both are valid; they just define "mod" differently.