Modulo Calculator

The modulo operation returns the remainder of a division. For positive numbers everyone agrees: 17 mod 5 = 2. But for negative numbers, languages and mathematicians diverge. The mathematical convention defines a mod b so the result is always in the range [0, |b|) — so -7 mod 3 = 2. The C/Go/JavaScript convention uses truncated division, so the remainder takes the sign of the dividend — meaning -7 % 3 = -1. This calculator shows both so you can pick the one your domain expects. It also supports floating-point moduli (used in angle wrapping, clock arithmetic, and signal processing) and explains each step of the calculation.

star 4.8
auto_awesome AI
New

Modulo Calculator calculator

percent a mod b

The number being divided. Integers or decimals, positive or negative.

The modulus. Must be non-zero.

analytics Result

Modulo (math, floored)
2
Always in range [0, |b|)
Remainder (C-style, truncated) 2

Sign follows dividend. Matches C, C++, Go, Java, JavaScript (%).

Quotient (floor)
3
Quotient (trunc)
3
Step by step

    tips_and_updates Tips

    • Use math modulo (floored) for modular arithmetic, hashing, and angle wrapping
    • Use C-style modulo (truncated) when matching behavior of C, C++, Go, Java, or JavaScript
    • For positive a and b the two conventions agree, so the distinction only matters with negatives
    • Modulo by zero is undefined — the divisor must be non-zero
    • Python's % operator uses math (floored) modulo: -7 % 3 = 2

    The Formula

    Math modulo uses floor division so the remainder is always 0 <= r < |b|. C-style modulo uses truncated division so the remainder has the same sign as the dividend.

    r = a - b * q, where q = floor(a/b) (math) or trunc(a/b) (C-style)

    lightbulb Variables Explained

    • a Dividend (the number being divided)
    • b Divisor (the modulus)
    • q Quotient (floored or truncated)
    • r Remainder / modulo result

    tips_and_updates Pro Tips

    1

    Use math modulo (floored) for modular arithmetic, hashing, and angle wrapping

    2

    Use C-style modulo (truncated) when matching behavior of C, C++, Go, Java, or JavaScript

    3

    For positive a and b the two conventions agree, so the distinction only matters with negatives

    4

    Modulo by zero is undefined — the divisor must be non-zero

    5

    Python's % operator uses math (floored) modulo: -7 % 3 = 2

    The modulo operation (often written as a mod b or a % b) returns the remainder after dividing one number by another and is one of the most frequently used operations in mathematics and computer science. A modulo calculator computes this remainder for any dividend and divisor, including negative numbers and decimals, while showing the step-by-step division process. For positive numbers, the result is straightforward: 17 mod 5 equals 2 because 17 divided by 5 is 3 with remainder 2. However, negative numbers introduce a critical distinction between two conventions: the mathematical (floored) convention always returns a non-negative result, while the programming (truncated) convention used in C, Java, and JavaScript preserves the sign of the dividend. This means negative 7 mod 3 equals 2 in mathematics but negative 1 in most programming languages. Understanding both conventions is essential for programmers, cryptographers, and anyone working with cyclic patterns, clock arithmetic, or hash functions.

    Math vs Programming Modulo: The Negative Number Problem

    The divergence between math and programming modulo conventions stems from how they handle integer division. Mathematical modulo uses floor division (rounding toward negative infinity), so the quotient of negative 7 divided by 3 floors to negative 3, giving remainder negative 7 minus 3 times negative 3 equals 2. Programming modulo uses truncated division (rounding toward zero), so the quotient truncates to negative 2, giving remainder negative 7 minus 3 times negative 2 equals negative 1.

    Python uniquely uses mathematical (floored) modulo, matching the convention expected in abstract algebra and number theory. C, C++, Java, JavaScript, Go, and Rust use truncated modulo. Ruby and Perl match Python's behavior.

    This distinction matters in real code: converting a negative angle to the range 0-359 requires math modulo (negative 90 mod 360 equals 270), but C's percent operator gives negative 90, requiring manual adjustment.

    Practical Applications of Modular Arithmetic

    Modular arithmetic underlies surprisingly many real-world systems:

    • Clock arithmetic is modular: 10 AM plus 5 hours equals 3 PM because 15 mod 12 equals 3.
    • Days of the week cycle with modulo 7.
    • Hash tables use modulo to map keys to bucket indices: hash(key) mod table_size.
    • Cryptographic algorithms like RSA depend entirely on modular exponentiation and the difficulty of finding modular inverses for large primes.
    • ISBN and credit card check digits use modulo operations for error detection (ISBN-13 uses mod 10, Luhn algorithm for credit cards).
    • Cyclic data structures like ring buffers use modulo to wrap array indices: next_index equals (current plus 1) mod buffer_size.

    Even simple tasks like determining if a number is even (n mod 2 equals 0) or extracting the last digit (n mod 10) rely on modulo.

    Modulo with Decimals and Special Cases

    The modulo operation extends naturally to real numbers using the same formula: r equals a minus b times floor(a divided by b). For example, 5.5 mod 2 equals 1.5 because floor(5.5/2) equals 2, and 5.5 minus 2 times 2 equals 1.5.

    Floating-point modulo is widely used in computer graphics for texture wrapping (u mod 1.0 maps any coordinate to the 0 to 1 range), angle normalization (theta mod 360 keeps angles within one full rotation), and signal processing for phase calculations.

    Important edge cases:

    • Modulo by zero is undefined and will cause division-by-zero errors in any language.
    • Modulo where the dividend is zero always returns zero (0 mod b equals 0 for any nonzero b).
    • When both operands are positive and the dividend is smaller than the divisor, the result equals the dividend (3 mod 7 equals 3).

    These properties make modulo predictable for positive numbers but require careful handling with negative or decimal inputs.

    What Is the Modulo Operation and How Does It Work?

    The modulo operation returns the remainder left over after dividing one integer by another, written as a mod b or a % b. Given a dividend a and a nonzero divisor b, it answers the question: after subtracting as many whole copies of b as possible from a, what is left?

    For example, 17 mod 5 = 2 because 5 fits into 17 three times (15), leaving 2. According to Wolfram MathWorld, the modulo function is formally defined as the remainder of a divided by b, and it always satisfies 0 <= r < |b| under the mathematical convention.

    This simple operation is a cornerstone of number theory, cryptography, and everyday programming, letting you detect divisibility, wrap values into a fixed range, and build cyclic behavior.

    The Modulo Formula: r = a - b * floor(a/b)

    To compute a mod b by hand, use the formula r = a - b * q, where q is the quotient. The two conventions differ only in how q is computed. Mathematical (floored) modulo sets q = floor(a/b), rounding toward negative infinity; C-style (truncated) modulo sets q = trunc(a/b), rounding toward zero.

    Worked through each case:

    • For 17 mod 5: floor(17/5) = 3, so r = 17 - 5*3 = 2.
    • For -7 mod 3 (math): floor(-7/3) = floor(-2.33) = -3, so r = -7 - 3*(-3) = 2.
    • For -7 % 3 (C-style): trunc(-2.33) = -2, so r = -7 - 3*(-2) = -1.

    As the NIST Digital Library of Mathematical Functions notes, floor and truncation coincide for positive operands, which is why both conventions agree when a and b are both positive.

    How to Compute Modulo Step by Step With a Worked Example

    Computing a mod b is a four-step process. Consider 23 mod 6.

    • Step 1: divide, 23 / 6 = 3.833.
    • Step 2: take the whole-number quotient, floor(3.833) = 3.
    • Step 3: multiply back, 3 * 6 = 18.
    • Step 4: subtract, 23 - 18 = 5.

    So 23 mod 6 = 5, and indeed 23 = 6*3 + 5. For a negative dividend like -23 mod 6 (math convention), floor(-23/6) = floor(-3.833) = -4, so -23 - 6*(-4) = -23 + 24 = 1.

    Khan Academy teaches this same subtraction method when introducing modular arithmetic, emphasizing that the remainder must always be smaller in magnitude than the divisor. Verify your answer by checking that a equals b times the quotient plus the remainder.

    What Is Modular Arithmetic and Congruence?

    Modular arithmetic is a system of arithmetic for integers where numbers wrap around after reaching a fixed value called the modulus. Two integers a and b are said to be congruent modulo n, written a ≡ b (mod n), when they leave the same remainder upon division by n, or equivalently when n divides their difference.

    For example, 17 ≡ 5 (mod 12) because both leave remainder 5 when divided by 12, which is why a 17:00 time reads as 5 PM on a 12-hour clock.

    Encyclopaedia Britannica describes modular arithmetic as arithmetic on a finite set of residues, and it was formalized by Carl Friedrich Gauss in his 1801 work Disquisitiones Arithmeticae. Congruence relations preserve addition and multiplication, making them the algebraic backbone of cryptography and error-detecting codes.

    Real-World Uses of Modulo in Programming and Everyday Life

    Modulo appears far more often than most people realize:

    • To test whether a number is even, check n mod 2 == 0; to extract the last digit of an integer, compute n mod 10.
    • Ring buffers and circular queues wrap their write position with index = (index + 1) mod capacity, so the pointer never runs off the end of the array.
    • Hash tables map keys to slots using hash mod table_size.
    • Time and calendar math is modular: the day of the week advances by (day + shift) mod 7, and clocks run on mod 12 or mod 24.
    • Check digits for ISBN-13 (mod 10) and bank routing numbers, plus the Luhn algorithm behind credit card validation, all rely on modulo to catch typos.
    • In cryptography, modular exponentiation powers RSA and Diffie-Hellman key exchange.

    Key Properties and Identities of the Modulo Operation

    Modulo obeys several useful algebraic properties that simplify calculation and proofs.

    The distributive-like rules for congruences state that (a + b) mod n = ((a mod n) + (b mod n)) mod n, and likewise (a * b) mod n = ((a mod n) * (b mod n)) mod n; these let you reduce large intermediate values before multiplying, which prevents overflow.

    Key identities to remember:

    • If the dividend is smaller than the divisor and both are positive, the result is just the dividend, so 3 mod 7 = 3.
    • A dividend of zero always yields zero: 0 mod b = 0.
    • Any multiple of the modulus gives zero, so 20 mod 5 = 0.
    • The result of a mod b is always strictly less than |b| under the mathematical convention.

    These identities, catalogued in resources like Wolfram MathWorld's congruence entry, make modular reduction both fast and predictable.

    Common Mistakes When Calculating Modulo

    Watch out for these frequent errors:

    • Assuming every language returns the same sign for negative operands. Python's -7 % 3 gives 2, but Java, C, and JavaScript give -1, so copying a formula between languages without adjusting can introduce subtle bugs, especially in angle wrapping or hashing.
    • Attempting modulo by zero, which is undefined and throws a runtime error in every language; always validate that the divisor is nonzero.
    • Confusing the quotient rounding direction, forgetting that floored and truncated division diverge for negatives.
    • Applying integer modulo to floating-point values without accounting for rounding can produce tiny errors; 5.5 mod 2 = 1.5 exactly, but accumulated floating-point drift may give 1.4999999.
    • Using n mod 2 == 1 to detect odd numbers fails for negatives in truncated languages, where -3 % 2 = -1; test n mod 2 != 0 instead.

    How Modulo Differs Across Programming Languages

    Because the remainder sign for negative operands is a design choice, languages fall into two camps.

    • Truncated (sign-of-dividend) modulo is used by C, C++, C#, Java, JavaScript, Go, Rust, and Swift, so -7 % 3 evaluates to -1 in all of them.
    • Floored (sign-of-divisor, always non-negative for positive divisors) modulo is used by Python, Ruby, and Perl, so -7 % 3 evaluates to 2.

    Some languages provide both: Python has math.fmod for the C-style truncated result, and many standard libraries expose a dedicated floor-mod function such as Java's Math.floorMod, which returns 2 for floorMod(-7, 3).

    When porting numeric code, always confirm which convention the target language uses. This calculator shows both the math (floored) and C-style (truncated) results side by side so you can match whichever your environment expects.

    Frequently Asked Questions

    sell

    Tags