In programming and mathematics, the modulus operator (%) plays a very important role but often gets overlooked. It’s a simple yet powerful tool once you know how to use it. Let’s dive into what it is, how it works, and where it is commonly used.
Table of Contents
What is Modulo?
Modulo (often written as % in programming) is an operation that finds the remainder when one number is divided by another.
In simple words:
a mod b = remainder when a is divided by b
PythonWhere:
- a = dividend (the number you divide)
- b = divisor (the number you divide by)
- result = remainder
General modulo formula:
a mod b = a − b×⌊a/b⌋
Pythonwhere ⌊⌋ is the floor (rounding down).
Derivation
Dividend = Divisor * Quotient + Remainder
Remainder = Dividend - Divisor * Quotient
Remainder = Dividend - Divisor * ⌊Dividend/Divisor⌋
PythonNote
Quotient = ⌊Dividend/Divisor⌋
Pythonwhere ⌊⌋ is give whole integer
Example Floor
x | ⌊x⌋ |
5.8 | 5 |
-5.8 | -6 |
2.1 | 2 |
-2.1 | -3 |
Note
- Rounding down
Example for floor function
⌊10/3⌋ = ⌊3.33⌋ = 3
⌊13/6⌋ = ⌊2.166⌋ = 2
PythonMod Example
Expression | Calculation | Shorthand | Result |
---|---|---|---|
10 % 3 | r = 10 – 3*⌊10/3⌋ | 10 ÷ 3 = 3 remainder 1 | 1 |
20 % 4 | r = 20 – 4*⌊20/4⌋ | 20 ÷ 4 = 5 remainder 0 | 0 |
15 % 6 | r = 15 – 6* ⌊15/6⌋ | 15 ÷ 6 = 2 remainder 3 | 3 |
5 % 8 | r = 5-8*⌊5/8⌋ | 5 ÷ 8 = 0 remainder 5 | 5 |
Example 2: Negative Values
Expression | Calculation | Result |
---|---|---|
10 % 3 | r = 10 – 3*⌊10/3⌋ r = 10 -3*3 r = 1 | 1 |
10 % -3 | r = 10 – (-3)*⌊10/-3⌋ r = 10 +3*(-4) r =-2 | -2 |
-10 % 3 | r = (-10) – 3* ⌊-10/3⌋ r = -10 -3*(-4) r = 2 | 2 |
-10 % -3 | r = (-10)-(-3)*⌊-10/-3⌋ r = -10+3*3 r =-1 | -1 |
Till now every thinks fine, as expected But
But
Case | Expression | Result in C | Result in Python | Math |
---|---|---|---|---|
1 | 10 % 3 | 1 | 1 | 1 |
2 | 10 % -3 | 1 | -2 | -2 |
3 | –10 % 3 | -1 | 2 | 2 |
4 | -10 % -3 | -1 | -1 | -1 |
Why case 2 and case 3 have different result
The Key Reason: Division Semantics
The modulo operation % is tightly connected to how integer division (/ or //) behaves. There are two common types:
In C:
- The sign of the result follows the numerator (left operand).
- It performs truncating division.
In Python:
- The sign of the result follows the denominator (right operand).
- It performs floor division: a % b == a – (a // b) * b
Type of Division | Rounds Toward | Example (-10 / 3 ) | Result |
---|---|---|---|
Truncating (C) | 0 (zero) | -10 / 3 → -3.333 → -3 → mod: -10 - (-3×3) = -1 | % = -1 |
Flooring (Python) | -∞ (down) | -10 // 3 → -4 → mod: -10 - (-4×3) = 2 | % = 2 |
- C uses truncating division, so % keeps the sign of the dividend (numerator).
- Python uses floor division, so % keeps the sign of the divisor (denominator).
Python
print(10/-3) #-3.333
print(int(10/-3)) #-3 This is equivalent to C/C++ Truncating
print(10//-3) #-4, Flooring
PythonWhy Different Choices?
- C (and Java, etc.) chose truncation because it’s simpler and faster on hardware.
- Python chose floor division because it’s mathematically consistent
Conclusion
The modulus operator is small but mighty. Whether you’re checking if a number is even, cycling through options, or managing boundaries, % can make your code cleaner and faster.
Once you start using it, you’ll find it everywhere in both basic and advanced programming problems!
1 thought on “Understanding the Modulo Operation”