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.

    Frequently Asked Questions

    sell

    Tags

    verified

    Data sourced from trusted institutions

    All formulas verified against official standards.