Perform Modulo with Negative Values in Python
Last Updated :
18 Sep, 2025
In Python, modulo operator % always ensures result has same sign as the divisor. The modulo operator (%) gives remainder after division. With negative numbers, different programming languages handle result differently, which often causes confusion.
This rule explains why -5 % 4 gives 3 instead of -1. Understanding this behavior is key when working with negative numbers in modulo operations.
Example:
Python
print(-5 % 4)
print(5 % -4)
Let’s carefully break this down.
Mathematics Behind Negative Modulo
Suppose we want to calculate -5 % 4. First, divide -5 by 4:
-5 / 4 = -1.25
If we take the floor of this division:
math.floor(-1.25) = -2
Now multiply back:
(-2 * 4) = -8
Then calculate the remainder:
-5 - (-8) = 3
So,
-5 % 4 = 3
Notice that instead of returning -1 (which some other languages would), Python gives 3 because it ensures the remainder has the same sign as the divisor (4, which is positive).
Distributive Property of Modulo
Python internally uses the distributive law of modulo:
(a + b) \bmod n \;=\; \big( (a \bmod n) + (b \bmod n) \big) \bmod n
For example:
-5 % 4 = (-2 * 4 + 3) % 4
= ( -8 + 3 ) % 4
= -5 % 4
= 3
This ensures that multiples of the divisor (like -2*4) reduce cleanly, and the remainder is adjusted to match the divisor’s sign.
More Examples
Let’s see some more calculations manually before checking them in Python.
-3 % 7 = 4
-5 % 2 = 1
-12 % 4 = 0
Python follows the same logic consistently.
Example 1: Basic Negative Modulo
Here we compare Python’s % result with the distributive property manually.
Python
res1 = -5 % 4
res2 = ((-2 * 4) + 3) % 4
print(res1)
print(res2)
Both methods give the same result.
Example 2: More Negative Modulo Calculations
Here we calculate modulo for two different negative values to see how Python handles them.
Python
res1 = -3 % 7
res2 = -12 % 4
print(res1)
print(res2)
Here, -12 % 4 = 0 because -12 is already a perfect multiple of 4.
Using math.fmod() for Alternative Behavior
Python also provides another way to perform modulo using the math.fmod() function.
Note: math.fmod() always returns a float value, even if both inputs are integers.
Let’s see an example:
Python
import math
x = -10
y = 3
result = math.fmod(x, y)
print(result)
Here, the remainder is -1 (negative) because the numerator -10 is negative.
% vs math.fmod() in Python
Operator | Sign of Result | Example (-10, 3) |
---|
% (Modulo) | Matches divisor | -10 % 3 = 2 |
math.fmod() | Matches numerator | math.fmod(-10, 3) = -1.0 |
So depending on whether you want the result to follow the divisor’s sign or the numerator’s sign, you can choose between % and math.fmod().
Real-World Use Cases
1. Clock Arithmetic (Modulo with Positive Result)
A common use case is handling time values, where wrapping around to positive values is important.
Python
hours = -5
print(hours % 12)
2. Signal Processing (fmod with Negative Result)
In scientific applications like signal processing, preserving the sign of the input is often necessary.
Python
import math
phase = -10
print(math.fmod(phase, 360))
Negative Module in Python
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice