Python math.fmod() Method
Python math.fmod(x, y)The method returns the remainder of x/y.
Python Version: 2.7
Syntax
The syntax of the math.fmod() method is as follows:
math.fmod(x, y)
Parameter Description:
- x-- Required, positive or negative number. The dividend. If x is not a number, TypeError is returned.
- y-- Required, positive or negative number. The divisor. If y is not a number, TypeError is returned.
Note:If x and y are 0, ValueError is returned.
Note:If y = 0, ValueError is returned.
Note:If x or y is not a number, TypeError is returned.
Return Value
A float value representing the remainder of x/y.
Example
The following example calculates the remainder:
Example
# Import the math package
import math
# Calculate the remainder
print(math.fmod(20, 4))
print(math.fmod(20, 3))
print(math.fmod(15, 6))
print(math.fmod(-10, 3))
# Error, ValueError: math domain error
print(math.fmod(0, 0))
import math
# Calculate the remainder
print(math.fmod(20, 4))
print(math.fmod(20, 3))
print(math.fmod(15, 6))
print(math.fmod(-10, 3))
# Error, ValueError: math domain error
print(math.fmod(0, 0))
Output result:
0.0
2.0
3.0
-1.0
Traceback (most recent call last):
File "/Users/EXAMPLE/example-test/test.py", line 9, in <module>
print(math.fmod(0, 0))
ValueError: math domain error
Other Extensions
Python math Module