What is math.modf(x) in Python?
The standard math module function math.modf() is used to split fractional and integer parts of argument value x, which returns a two-item tuple of float type. For the negative value of x, a negative sign will appear with both values of output in a tuple.
Syntax
math.modf(x)
Parameters
The math.modf() function accepts only a single value as input.
x: a numeric expression.
Return value
This method returns a two-item tuple, with fractional and integer parts in it, i.e., (fractional_part, integer_part)
Demo code
In the code snippet below:
x= math.pi: On passingpivalue (3.141592…) as an argument, it will return a tuple(0.14159265358979312, 3.0)containing fractional and integer parts.x= 786.11: On passing a positive value ofx, it will return a tuple of positive values.x= -786.69: On passing a negative value ofx, it will return a tuple of negative values.
To check the output, execute now.
# include math moduleimport math# ---- code is starting here ----print("math.modf(math.pi) : ", math.modf(math.pi))print("math.modf(786.11) : ", math.modf(786.11))print("math.modf(-786.69) : ", math.modf(-786.69))