Report

Help us improve this tool

Modulo of Negative Numbers Calculator

Calculate modulo operations with negative dividends and divisors across Python/floored, C/truncated, Euclidean, and ceiled conventions.

O M T

Understanding the Modulo of Negative Numbers

The modulo operation finds the remainder \(r\) after division of one number by another (the dividend \(a\) divided by the divisor \(n\)), satisfying the fundamental division identity:

$$a = q \times n + r$$

When both \(a\) and \(n\) are positive integers, the quotient \(q\) and remainder \(r\) are uniquely defined and universally agreed upon. However, when the dividend \(a\) or divisor \(n\) is negative, different programming languages and mathematical disciplines adopt different conventions for rounding the quotient \(q\), resulting in distinct remainder values \(r\).

You can also explore our standard Modulo Calculator, Modular Multiplicative Inverse Calculator, and Is Modulo Associative Calculator.

The Four Main Modulo Conventions

1. Floored Division (Python, Ruby, Perl, Knuth's Definition)

In floored division, the quotient is rounded toward negative infinity using the floor function:

$$q = \lfloor a / n \rfloor, \quad r = a - n \times \lfloor a / n \rfloor$$

Sign Rule: The remainder \(r\) always has the same sign as the divisor \(n\) (or is 0).

Example: For \(-7 \bmod 3\):

$$q = \lfloor -7 / 3 \rfloor = \lfloor -2.333 \rfloor = -3 \implies r = -7 - (3 \times -3) = -7 + 9 = 2$$

2. Truncated Division (C, C++, Java, JavaScript %, C#, Rust, Go)

In truncated division, the quotient is rounded toward zero (discarding fractional digits):

$$q = \text{trunc}(a / n), \quad r = a - n \times \text{trunc}(a / n)$$

Sign Rule: The remainder \(r\) always has the same sign as the dividend \(a\) (or is 0).

Example: For \(-7 \bmod 3\) in C or JavaScript:

$$q = \text{trunc}(-7 / 3) = -2 \implies r = -7 - (3 \times -2) = -7 + 6 = -1$$

3. Euclidean Division (Pure Mathematics & Number Theory)

Euclidean division requires that the remainder must strictly satisfy \(0 \le r < |n|\), ensuring that the remainder is always non-negative:

$$0 \le r < |n|, \quad q = \frac{a - r}{n}$$

Example: For \(-7 \bmod 3\), the non-negative remainder in \(\{0, 1, 2\}\) is \(r = 2\) because \(-7 = (-3 \times 3) + 2\).

4. Ceiled Division

In ceiled division, the quotient is rounded toward positive infinity:

$$q = \lceil a / n \rceil, \quad r = a - n \times \lceil a / n \rceil$$

Sign Rule: The remainder \(r\) has the opposite sign of the divisor \(n\) (or is 0).

Comparison Summary Table

Here is how the four conventions evaluate \(-7 \bmod 3\) and \(7 \bmod -3\):

Convention -7 mod 3 (q, r) 7 mod -3 (q, r) Primary Ecosystems
Floored q = -3, r = 2 q = -3, r = -2 Python, Ruby, MATLAB, R
Truncated q = -2, r = -1 q = -2, r = 1 C, C++, Java, JS, Rust, Go, PHP
Euclidean q = -3, r = 2 q = -2, r = 1 Number Theory, Pascal, Julia
Ceiled q = -2, r = -1 q = -2, r = 1 Specialized scheduling

Frequently Asked Questions

Why does Python give a positive remainder for negative dividends?

Python uses floored division (\(//\) and \(\%\)), which rounds the quotient down toward negative infinity. This ensures that the remainder always shares the sign of the divisor, which is practical for cyclical indexing and clock arithmetic.

Why does JavaScript % produce negative numbers?

In JavaScript, the \(\%\) operator is the remainder operator (truncated division), not a mathematical modulo operator. It preserves the sign of the dividend \(a\).

How can I get Python-style modulo in JavaScript?

You can write a simple helper function: const mod = (a, n) => ((a % n) + n) % n; to compute positive Euclidean/floored modulo.

Is \(a \bmod n\) ever undefined?

Modulo is undefined whenever the divisor \(n = 0\), because division by zero is mathematically undefined.