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

ParameterRequiredDescription
xYesA numeric value.
yYesA numeric value.

If x is infinity, then fmod(x, y) raises ValueError.

If x is nan, then fmod(x, y) returns nan.

Examples

Floating point reminder of floating point values division.

Python Program

import math

x = 5.6
y = 1.1
result = math.fmod(x, y)
print('fmod(x, y) :', result)
Run

Output

fmod(x, y) : 0.0999999999999992

Floating point reminder of integer division.

Python Program

import math

x = 5
y = 2
result = math.fmod(x, y)
print('fmod(x, y) :', result)
Run

Output

fmod(x, y) : 1.0

Floating point reminder of division when numerator is infinity.

Python Program

import math

x = math.inf
y = 2
result = math.fmod(x, y)
print('fmod(x, y) :', result)
Run

Output

ValueError: math domain error

Floating point reminder of division when denominator is infinity.

Python Program

import math

x = 5
y = math.inf
result = math.fmod(x, y)
print('fmod(x, y) :', result)
Run

Output

fmod(x, y) : 5.0

Floating point reminder of division when numerator is nan.

Python Program

import math

x = math.nan
y = 5
result = math.fmod(x, y)
print('fmod(x, y) :', result)
Run

Output

fmod(x, y) : nan

Summary

In this Python Examples tutorial, we learned the syntax of, and examples for math.fmod() function.