Python Math fmod() Function Example
Python fmod() is an inbuilt function under the math library that is used to find a module of two given numbers. Means, this function finds x%y for a given any two numbers x and y. Please remember that this function is advised to use when you want to find a module of any float number. Because, in Python, if you use x%y for any float type number with different signed values, the result may differ from using fmod(x,y).
The math.fmod() is a method from the Standard math Library of Python Programming Language. Python math fmod() function is used to calculate the module value of given parameters x and y. The math.fmod() method is a library method of the math module, and it is used to find a modulus for given numbers, where the first parameter is a dividend, and the second parameter is the divisor.
It accepts two arguments and will return modulus in the float type.
Note: The math.fmod() can be used to get the modules/remainder of positive and negative integers and positive and negative floats.
The function takes two arguments, which are x and y (both can be positive or negative), which finds x%y.
The fmod() function returns a floating-point number value after calculating the module of the given two numbers.
Please note that,
- If x and y are both zero, then this function returns a ValueError.
- If the second argument means y is zero, then also it returns a ValueError.
- If any of x or y is not a number, then this function returns a TypeError.
Python fmod() Function Compatibility
Python 2.x – Yes
Python 3.x – Yes
See the following code.
# app.py # Importing math library import math # Demonstrating working of fmod() # Using different values of x and y # When both are positive x = 12 y = 9 print("Module of ", x, " and ", y, "is: ", math.fmod(x, y)) # When any one of them are negative x = -16 y = 3 print("Module of ", x, " and ", y, "is: ", math.fmod(x, y)) # When both are negative x = -65 y = -31 print("Module of ", x, " and ", y, "is: ", math.fmod(x, y)) # When second argument (y) is 0 x = 10 y = 0 print("Module of ", x, " and ", y, "is: ", math.fmod(x, y))
Module of 12 and 9 is: 3.0 Module of -16 and 3 is: -1.0 Module of -65 and -31 is: -3.0 Traceback (most recent call last): File "fmod.py", line 24, in <module> print("Module of ",x," and ",y, "is: ",math.fmod(x,y)) ValueError: math domain error
In the above code, we have declared two variables x and y and given their different values in different cases.
After that, we have a printed module of each case, we can see that the answer is positive only when both x and y are positive, and except that in all cases, the answer is negative.
Also, we can see that in each case, the answer is in floating-point.
However, in the last case, when we gave the value of y is 0, we got a ValueError.
The return fmod(x, y), as defined by the platform C library. Note that the Python expression x % y may not return the same result.
The C standard intends that fmod(x, y) be exactly (mathematically; to infinite precision) equal to x – n*y for some integer n such that the result has a same sign as x and magnitude less than abs(y).
Python’s x % y returns the result with the sign of y instead, and may not be precisely computable for float arguments.