Last verified · v1.0
Calculator · math
Modulo Of Negative Numbers Calculator
Compute modulo of negative numbers across floored, truncated, and Euclidean conventions. Compare Python, C, Java, and JavaScript results side by side.
Inputs
a mod n
—
Explain my result
Get a plain-English breakdown of your result with practical next steps.
The formula
How the
result is
computed.
Understanding the Modulo of Negative Numbers
The modulo operation finds the remainder after dividing one integer by another. For positive integers, the result is universally agreed upon. When negative numbers appear as the dividend or divisor, however, three competing conventions produce different answers — and using the wrong one silently corrupts cryptographic algorithms, graphics coordinate systems, date arithmetic, and ring-buffer implementations. The modulo of negative numbers calculator evaluates all major conventions simultaneously so programmers and mathematicians can instantly compare behavior across environments.
The Core Formula: Floored Division
The convention most consistent with classical number theory uses floored division, documented rigorously in Building Blocks: Number Theory (Chapter 4) by Margaret Fleck, University of Illinois:
a mod n = a − n · ⌊a / n⌋
The variables in this formula are defined as follows:
- a — the dividend; any integer, positive or negative (e.g., −13, 7, −100)
- n — the modulus or divisor; any non-zero integer that defines the cycle length (e.g., 4, −3)
- ⌊ · ⌋ — the floor function, which rounds toward negative infinity (so ⌊−2.333⌋ = −3, not −2)
The floor function is the critical detail: rounding toward −∞ means the remainder always carries the same sign as the divisor n under this convention. Python, Ruby, and R all implement floored division natively via their % operator.
The Three Major Modulo Conventions
As established in Section 2.1: Shift Ciphers and Modular Arithmetic (Radford University, Department of Mathematics), different computational systems define modulo differently for negative inputs. Selecting the wrong convention is a common source of off-by-one errors in implementations that cross language boundaries.
1. Floored Division — Python, Ruby, R
The quotient is rounded toward −∞. The result always carries the sign of the divisor. For −7 mod 3: ⌊−7/3⌋ = ⌊−2.333⌋ = −3, so −7 − 3·(−3) = −7 + 9 = 2. For 7 mod −3: ⌊7/−3⌋ = ⌊−2.333⌋ = −3, so 7 − (−3)·(−3) = 7 − 9 = −2.
2. Truncated Division — C, C++, Java, JavaScript, Go, Swift
The quotient is rounded toward zero. The result carries the sign of the dividend. For −7 mod 3: trunc(−7/3) = trunc(−2.333) = −2, so −7 − 3·(−2) = −7 + 6 = −1. For 7 mod −3: trunc(7/−3) = trunc(−2.333) = −2, so 7 − (−3)·(−2) = 7 − 6 = 1. This is the behavior of the % operator in C-family languages.
3. Euclidean Division — Formal Number Theory, Cryptography
The Euclidean convention guarantees a strictly non-negative remainder regardless of the signs of either operand. Formally: r = a − |n| · ⌊a / |n|⌋, ensuring r ≥ 0 always holds. For −7 mod 3: result = 2. For 7 mod −3: result = 1. This convention is preferred in modular group theory and RSA key generation.
Worked Examples Across All Three Conventions
Example A: −13 mod 4
- Floored: ⌊−13/4⌋ = ⌊−3.25⌋ = −4 → −13 − 4·(−4) = 3
- Truncated: trunc(−13/4) = −3 → −13 − 4·(−3) = −1
- Euclidean: r = 3 (non-negative, same as floored when n > 0)
Example B: 13 mod −4
- Floored: ⌊13/−4⌋ = ⌊−3.25⌋ = −4 → 13 − (−4)·(−4) = −3
- Truncated: trunc(13/−4) = −3 → 13 − (−4)·(−3) = 1
- Euclidean: r = 1 (always non-negative regardless of sign of n)
Example C: −13 mod −4
- Floored: ⌊−13/−4⌋ = ⌊3.25⌋ = 3 → −13 − (−4)·3 = −1
- Truncated: trunc(3.25) = 3 → −13 − (−4)·3 = −1 (same as floored here)
- Euclidean: r = 3 (always non-negative)
Real-World Applications
- Cryptography: RSA encryption, Diffie-Hellman key exchange, and elliptic-curve protocols perform group operations in Z/nZ, requiring non-negative residues guaranteed by Euclidean modulo.
- Circular data structures: Ring buffers, circular queues, and 2D tile-map wrapping use floored modulo to prevent negative array indices from causing out-of-bounds errors.
- Calendar arithmetic: Computing the day-of-week for dates in the past involves negative offsets; floored modulo ensures the result stays within 0–6 without extra correction logic.
- Digital signal processing: Phase accumulators and waveform lookup tables rely on predictable modulo sign behavior across both positive and negative sample-offset values.
- Hash table probing: Open-addressing hash tables use modulo to compute bucket indices; a negative result from truncated modulo causes an immediate array-bounds fault in most languages.
Always verify which convention the target language or library applies before implementing modular arithmetic with negative operands. A single incorrect assumption can produce results that pass unit tests on positive inputs yet fail silently in production when negative values appear.
Reference