CalcTune
📐
Math · Arithmetic

Modulo Calculator

Calculate the remainder and quotient of integer division. Enter a dividend and divisor to see the result in both mathematical (floored) and programming (truncated) conventions.

Example values — enter yours above
a mod b = a - b × floor(a/b)
17 mod 5 = 2
Floored (Math)
2
Remainder
3
Quotient
Verification: 5 × 3 + 2 = 17

Understanding the Modulo Operation: Remainders, Quotients, and Conventions

The modulo operation is one of the most widely used operations in both mathematics and computer science. At its core, it computes the remainder when one integer is divided by another. Written as a mod b, it answers the question: after dividing a by b as many times as possible, what is left over? Despite this seemingly simple definition, the modulo operation has deep connections to number theory, cryptography, and algorithm design, and its behavior with negative numbers introduces subtleties that every programmer and mathematician should understand.

This guide provides a comprehensive overview of the modulo operation, including its definition, the distinction between floored and truncated division, practical applications, and common pitfalls when working with negative numbers in different programming languages.

Definition and Basic Formula

Given two integers a (the dividend) and b (the divisor, where b is not zero), the modulo operation a mod b produces the remainder r such that a = b × q + r, where q is the quotient (an integer) and 0 <= r < |b| in the standard mathematical convention. The quotient is computed as q = floor(a / b), meaning the largest integer less than or equal to the exact division result.

For positive numbers, this is straightforward. For example, 17 mod 5: the quotient is floor(17/5) = 3, and the remainder is 17 - 5 × 3 = 2. We verify: 5 × 3 + 2 = 17. Similarly, 25 mod 7 = 4, because floor(25/7) = 3 and 25 - 7 × 3 = 4.

Floored vs. Truncated Division

The behavior of the modulo operation with negative numbers depends on how the quotient is computed. There are two main conventions: floored division and truncated division.

In floored division (used in mathematics and languages like Python), the quotient is computed using the floor function, which rounds toward negative infinity. This ensures the remainder always has the same sign as the divisor. For example, -7 mod 3 = 2 (floored), because floor(-7/3) = -3 and -7 - 3 × (-3) = -7 + 9 = 2.

In truncated division (used in C, Java, JavaScript's % operator, and many other languages), the quotient is computed by truncating toward zero. This means the remainder has the same sign as the dividend. For example, -7 % 3 = -1 in JavaScript, because trunc(-7/3) = -2 and -7 - 3 × (-2) = -7 + 6 = -1.

Both conventions are valid—they simply define the remainder differently. For positive dividends and divisors, the two conventions give the same result. The difference matters only when one or both operands are negative.

The Verification Property

Regardless of which convention is used, the fundamental relationship a = b × q + r always holds. This is the Euclidean division theorem: for any integers a and b (with b not zero), there exist unique integers q (quotient) and r (remainder) such that a = b × q + r and 0 <= r < |b|. The calculator displays this verification to confirm the correctness of the result.

This property is useful for checking arithmetic and is the basis for many proofs in number theory. It guarantees that division into quotient and remainder is always well-defined and unique (under each convention).

Applications in Computer Science

The modulo operation is ubiquitous in programming. Common applications include: determining if a number is even or odd (n % 2), cycling through array indices (i % length), implementing circular buffers and ring data structures, computing hash functions, and formatting output (e.g., inserting a newline every N items).

In cryptography, modular arithmetic is the foundation of public-key encryption algorithms such as RSA and Diffie-Hellman. Operations like modular exponentiation (computing a^b mod n efficiently) are central to these systems. The security of RSA depends on the difficulty of factoring large numbers, but all the encryption and decryption operations are performed using modular arithmetic.

Modular arithmetic also appears in checksum algorithms (ISBN, credit card validation via the Luhn algorithm), error-correcting codes, and pseudorandom number generators.

Applications in Mathematics

In number theory, modular arithmetic forms the basis of congruence relations. Two integers a and b are said to be congruent modulo n (written a ≡ b mod n) if n divides a - b. This relation is an equivalence relation and partitions the integers into n residue classes.

Congruences are used to prove divisibility rules, solve systems of linear congruences (via the Chinese Remainder Theorem), and study the structure of integers. Fermat's Little Theorem, Euler's theorem, and quadratic reciprocity are all stated and proved in terms of modular arithmetic. These results have deep implications for the distribution of prime numbers and the solvability of Diophantine equations.

Negative Number Behavior Across Languages

Different programming languages handle the modulo of negative numbers differently, which is a common source of bugs. JavaScript, C, C++, and Java use truncated division for the % operator, so the result has the sign of the dividend. Python uses floored division for the % operator, so the result has the sign of the divisor. Ruby also uses floored division.

To convert a truncated remainder to a floored remainder, you can use the formula: floored_mod = ((a % b) + b) % b. This pattern is frequently used in JavaScript and C when you need a non-negative result, for example when computing array indices from potentially negative offsets.

Common Use Cases and Examples

Clock arithmetic is a natural example of modular arithmetic. Hours wrap around after 12 or 24: if it is 10 o'clock and you add 5 hours, you get 15 mod 12 = 3 o'clock. Days of the week cycle modulo 7. Calendar computations, scheduling algorithms, and timekeeping systems all rely on modular arithmetic.

In everyday programming, checking divisibility is one of the most common uses: if n % 3 == 0, then n is divisible by 3. FizzBuzz, a classic programming exercise, is entirely built on the modulo operation. Modulo is also used to constrain values within a range, implement wraparound behavior in games and simulations, and distribute work among parallel processes.

Frequently Asked Questions

What does the modulo operation do?

The modulo operation (a mod b) computes the remainder when a is divided by b. For example, 17 mod 5 = 2, because 17 = 5 × 3 + 2. The quotient is 3 and the remainder is 2. The operation is fundamental in mathematics and computer science.

What is the difference between floored and truncated modulo?

Floored modulo uses floor division (rounding toward negative infinity), so the remainder has the same sign as the divisor. Truncated modulo uses truncation toward zero, so the remainder has the same sign as the dividend. For positive numbers, they give the same result. The difference matters for negative numbers: -7 mod 3 = 2 (floored) vs. -1 (truncated).

Why can't you divide by zero?

Division by zero is undefined in mathematics because there is no number that, when multiplied by 0, gives any nonzero dividend. The modulo operation inherits this restriction since it relies on division. Attempting to compute a mod 0 will result in an error in most programming languages.

How does JavaScript handle negative modulo?

JavaScript's % operator uses truncated division, meaning the result has the same sign as the dividend. For example, -7 % 3 = -1 in JavaScript. To get a non-negative (floored) result, use the pattern ((a % b) + b) % b, which gives -7 mod 3 = 2.

What are common uses of the modulo operation?

Common uses include: checking if a number is even or odd (n % 2), cycling through array indices, clock arithmetic (hours mod 12 or 24), implementing circular buffers, cryptographic algorithms (RSA), hash functions, checksum validation (Luhn algorithm), and distributing work across parallel processes.