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

ParameterRequiredDescription
xYesA numeric value.

Examples

Fractional and integer parts of x=5.2.

Python Program

import math

x = 5.2
result = math.modf(x)
print('modf() :', result)
print('Fractional Part :', result[0])
print('Integer Part    :', result[1])
Run

Output

modf() : (0.20000000000000018, 5.0)
Fractional Part : 0.20000000000000018
Integer Part    : 5.0

Fractional and integer parts of x=infinity.

Python Program

import math

x = math.inf
result = math.modf(x)
print('modf() :', result)
print('Fractional Part :', result[0])
print('Integer Part    :', result[1])
Run

Output

modf() : (0.0, inf)
Fractional Part : 0.0
Integer Part    : inf

Fractional and integer parts of x=NaN (Not a Number).

Python Program

import math

x = math.nan
result = math.modf(x)
print('modf() :', result)
print('Fractional Part :', result[0])
print('Integer Part    :', result[1])
Run

Output

modf() : (nan, nan)
Fractional Part : nan
Integer Part    : nan

Summary

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