Python fmod() Function: Complete Example

The math.fmod() is a method from the Standard math Library of Python Programming Language. The fmod() function calculates the module value of given parameters x and y.

Python fmod()

Python fmod() is a built-in function under the math library used to find a module of two given numbers. The fmod() function accepts two arguments and will return modulus in the float type.

The math.fmod() method is a library method of the math module. It is used to find a modulus for given numbers, where the first parameter is a dividend, and the second parameter is the divisor. This means the fmod() 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).

Note: The math.fmod() can be used to get the modules/remainder of positive and negative integers and positive and negative floats.

Syntax

math.fmod(x,y)

Arguments

The fmod() function takes two arguments: x and y (both can be positive or negative), which find x%y.

Return Value

The fmod() function returns a floating-point number value after calculating the module of the given two numbers.

Please note that, 

  1. If x and y are both zero, this function returns a ValueError.
  2. If the second argument means y is zero, it also returns a ValueError.
  3. If any of x or y is not a number, this function returns a TypeError.

Python fmod() Function Compatibility

Python 2.x – Yes
Python 3.x – Yes

Programming Example

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))

Output

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, given their different values in different cases.

After that, we have a printed module of each case, and we can see that the answer is positive only when both x and y are positive, except that the answer is negative in all cases.

Also, we can see that in each case, the answer is in a floating-point.

However, in the last case, when we gave the value of y is 0, we got a ValueError.

Conclusion

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 the 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.

See Also

Python factorial

Python math.fabs()

Python math copysign()

Python math functions

Python math.sqrt()

Python math.floor()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.