Python math.modf() - Fraction and Integer Parts of a Number
Python math.modf()
math.modf(x) function returns the fractional and integer parts of x.
If x is signed, then both fractional and integer parts carry the sign of x.
Syntax
The syntax to call modf() function is
math.modf(x)
where
Parameter | Required | Description |
---|---|---|
x | Yes | A numeric value. |
Examples
1. Fraction and integer parts of a float value
In the following program, we take a float value of 5.2 in variable x
, and find its fraction and integer parts.
Python Program
import math
x = 5.2
result = math.modf(x)
print('modf() :', result)
print('Fractional Part :', result[0])
print('Integer Part :', result[1])
Output
modf() : (0.20000000000000018, 5.0)
Fractional Part : 0.20000000000000018
Integer Part : 5.0
2. Fraction and integer parts of infinity
In the following program, we take the value of infinity in variable x
, and find its fraction and integer parts.
Python Program
import math
x = math.inf
result = math.modf(x)
print('modf() :', result)
print('Fractional Part :', result[0])
print('Integer Part :', result[1])
Output
modf() : (0.0, inf)
Fractional Part : 0.0
Integer Part : inf
3. Fraction and integer parts of nan
In the following program, we take nan in variable x
, and find its fraction and integer parts.
Python Program
import math
x = math.nan
result = math.modf(x)
print('modf() :', result)
print('Fractional Part :', result[0])
print('Integer Part :', result[1])
Output
modf() : (nan, nan)
Fractional Part : nan
Integer Part : nan
Summary
In this Python Math tutorial, we learned the syntax of, and examples for math.modf() function.