terican

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.

FreeInstantNo signupOpen source

Inputs

a mod n

Explain my result

0/3 free

Get a plain-English breakdown of your result with practical next steps.

a mod n

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

Frequently asked questions

What is the modulo of a negative number?
The modulo of a negative number is the remainder left after dividing that number by the modulus. For example, -7 mod 3 equals 2 under the floored convention used by Python, or -1 under the truncated convention used by C and JavaScript. The numeric result depends entirely on which rounding rule — floored, truncated, or Euclidean — is applied to the quotient before computing the remainder.
Why does -7 mod 3 give different answers in Python and JavaScript?
Python uses floored division, rounding the quotient toward negative infinity: floor(-2.333) = -3, giving -7 - 3*(-3) = 2. JavaScript uses truncated division, rounding toward zero: trunc(-2.333) = -2, giving -7 - 3*(-2) = -1. Both computations are internally consistent, but the different rounding strategies produce different remainders. This divergence only appears when at least one operand is negative.
How does Python calculate modulo with negative numbers?
Python applies the formula a % n = a - n * floor(a/n), where the floor function rounds toward negative infinity. This guarantees the result always shares the sign of the divisor n. For example, -7 % 3 = 2 and 7 % -3 = -2 in Python. This behavior makes Python's modulo reliable for circular indexing, clock arithmetic, and cryptographic residue calculations that require non-negative results when n is positive.
How can a non-negative modulo result be forced in JavaScript?
JavaScript's native % operator uses truncated division, so it can return negative remainders. To force a non-negative result equivalent to Python's floored modulo, use the expression ((a % n) + n) % n. For example, ((-7 % 3) + 3) % 3 evaluates to ((-1) + 3) % 3 = 2 % 3 = 2, matching Python's output. This two-step workaround is the standard idiom in JavaScript for safe circular indexing and hash-bucket calculations.
What is the difference between truncated and floored modulo?
Truncated modulo rounds the quotient toward zero, so the remainder inherits the sign of the dividend. Floored modulo rounds the quotient toward negative infinity, so the remainder inherits the sign of the divisor. For all-positive inputs the two conventions agree. The divergence appears with negative operands: -13 mod 4 yields -1 under truncated division but 3 under floored division. Formal mathematics and Python favor floored modulo; C-family languages favor truncated.
When should Euclidean modulo be used instead of floored or truncated?
Euclidean modulo should be chosen whenever a strictly non-negative remainder is required regardless of the signs of both operands. It is the standard convention in formal number theory, RSA key generation, elliptic-curve cryptography, and modular group operations. Unlike floored modulo, Euclidean modulo remains non-negative even when the divisor n is negative — for example, 13 mod -4 = 1, not -3. This makes it the most mathematically robust choice for algorithms that depend on residues being elements of the set {0, 1, ..., |n|-1}.