all Python 3Numbers & mathfunction
math.fmod
remainder
The C-library fmod remainder: the sign matches x (differs from %).
Syntax
math.fmod(...)
The difference from % is not cosmetic: fmod follows C and takes the sign of the dividend, while % takes the sign of the divisor, so math.fmod(-10, 3) is -1.0 whereas -10 % 3 is 2. Use % for integers and fmod for floats — fmod is exact by construction, while % loses accuracy when the operands differ wildly in magnitude. The result is always a float, and a zero divisor raises ValueError, not ZeroDivisionError.
Examples
import math
print(math.fmod(10, 3)) # → 1.0
print(math.fmod(-10, 3)) # → -1.0
print(-10 % 3) # → 2
print(math.fmod(10, -3)) # → 1.0
print(math.fmod(7.5, 2)) # → 1.5
print(math.fmod(10, 0)) # → ValueError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy