Python math.fmod() - Modulus Function
Python math.fmod()
math.fmod(x, y) function returns the floating point remainder of the division operation x/y.
Syntax
The syntax to call fmod() function is
math.fmod(x, y)where
| Parameter | Required | Description |
|---|---|---|
| x | Yes | A numeric value. |
| y | Yes | A numeric value. |
If x is infinity, then fmod(x, y) raises ValueError.
If x is nan, then fmod(x, y) returns nan.
Examples
1. Floating point reminder of floating point values division
In the following program, we take two floating point numbers in x, y, and find the floating point reminder of of the division x/y.
Python Program
import math
x = 5.6
y = 1.1
result = math.fmod(x, y)
print('fmod(x, y) :', result)Output
fmod(x, y) : 0.09999999999999922. Floating point reminder of integer division
In the following program, we take two integer numbers in x, y, and find the floating point reminder of of the division x/y.
Python Program
import math
x = 5
y = 2
result = math.fmod(x, y)
print('fmod(x, y) :', result)Output
fmod(x, y) : 1.03. Floating point reminder of division when numerator is infinity
In the following program, we take values in x, y, such that numerator is infinity, and pass these values as arguments to math.fmod() function, and find what it does.
Python Program
import math
x = math.inf
y = 2
result = math.fmod(x, y)
print('fmod(x, y) :', result)Output
ValueError: math domain error4. Floating point reminder of division when denominator is infinity
In the following program, we take values in x, y, such that the denominator is infinity, and pass these values as arguments to math.fmod() function, and find what it returns.
Python Program
import math
x = 5
y = math.inf
result = math.fmod(x, y)
print('fmod(x, y) :', result)Output
fmod(x, y) : 5.05. Floating point reminder of division when numerator is nan
In the following program, we take values in x, y, such that the numerator is nan, and pass these values as arguments to math.fmod() function, and find what it returns.
Python Program
import math
x = math.nan
y = 5
result = math.fmod(x, y)
print('fmod(x, y) :', result)Output
fmod(x, y) : nanSummary
In this Python Math tutorial, we learned the syntax of, and examples for math.fmod() function.