Open In App

Python math function | modf()

Last Updated : 14 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

modf() function is an inbuilt function in Python that returns the fractional and integer parts of the number in a two-item tuple. Both parts have the same sign as the number. The integer part is returned as a float. Syntax :

modf(number) 

Time Complexity: O(1)

Auxiliary Space: O(1)

Parameter :

There is only one mandatory parameter which is the number. 

Returns : This method returns the fractional and integer parts of number in a two-item tuple. Both parts have the same sign as the number. The integer part is returned as a float. Exception :

TypeError:  If anything other than a float number is passed, it returns a type error. 

Below is the Python3 implementation of modf() method : Code #1 

Python3




# Python3 program to demonstrate the function modf()
 
# This will import math module
import math  
 
# modf() function used with a positive number
print("math.modf(100.12) : ", math.modf(100.12))
 
# modf() function used with a negative number
print("math.modf(-100.72) : ", math.modf(-100.72))
 
print("math.modf(2) : ", math.modf(2))


Output :

math.modf(100.12) :  (0.12000000000000455, 100.0)
math.modf(-100.72) :  (-0.7199999999999989, -100.0)
math.modf(2) :  (0.0, 2.0)

  Code #2 : TypeError 

Python3




# Python3 program to demonstrate the 
# error in function modf()
 
# This will import math module
import math  
 
# modf() function used with a positive number
print("math.modf(100.12) : ", math.modf("100.12"))


Output : 

Traceback (most recent call last):
  File "/home/fa6d7643de17bafe9a0e0693458e4bdb.py", line 9, in 
    print("math.modf(100.12) : ", math.modf("100.12"))
TypeError: a float is required

  Code #3

Python3




# Python3 program to demonstrate the
# error in function modf()
 
# This will import math module
from math import modf
 
lst = [3.12, -5.14, 13.25, -5.21]
tpl = (33.12, -15.25, 3.15, -31.2)
 
 
# modf() function on elements of list
print("modf() on First list element : ", modf(lst[0]))
print("modf() on third list element : ", modf(lst[2]))
 
# modf() function on elements of tuple
print("modf() on Second tuple element : ", modf(tpl[1]))
print("modf() on Fourth tuple element : ", modf(tpl[3]))


Output :

modf() on First list element :  (0.1200000000000001, 3.0)
modf() on third list element :  (0.25, 13.0)
modf() on Second tuple element :  (-0.25, -15.0)
modf() on Fourth tuple element :  (-0.1999999999999993, -31.0)

Practical Application : Given two float numbers, multiply the fractional part and return the answer. Code #4 : 

Python3




# Python3 program to demonstrate the
# application of function modf()
 
# This will import math module
import math  
 
# modf() function to multiply fractional part
a = math.modf(11.2)
b = math.modf(12.3)
 
# Multiply the fractional part as is stored 
# in 0th index of both the tuple
print(a[0]*b[0])


Output :

0.05999999999999993


Similar Reads

Python math.sqrt() function | Find Square Root in Python
sqrt() function returns square root of any number. It is an inbuilt function in Python programming language. In this article, we will learn more about the Python Program to Find the Square Root. sqrt() Function We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root
3 min read
Python math function | hypot()
hypot() function is an inbuilt math function in Python that return the Euclidean norm, [Tex]\sqrt{(x*x + y*y)} [/Tex]. Syntax : hypot(x, y) Parameters : x and y are numerical values Returns : Returns a float value having Euclidean norm, sqrt(x*x + y*y). Error : When more than two arguments are passed, it returns a TypeError. Note : One has to impor
2 min read
Python | math.fabs() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.fabs() function returns the absolute value of the number. Syntax: math.fabs(x) Parameter: x: This is a numeric expression. Returns: the absolute value of the number. Time Complexity: O(1) Auxiliary Space: O(1) Code #1: C/C++
1 min read
Python | math.floor() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.floor() function returns the largest integer not greater than x. If number is already integer, same number is returned. Syntax: math.floor(x) Parameter: x: This is a numeric expression. Returns: largest integer not greater th
1 min read
Python math function | copysign()
math.copysign() is a function exists in Standard math Library of Python. This function returns a float value consisting of magnitude from parameter x and the sign (+ve or -ve) from parameter y. Syntax : math.copysign(x, y) Parameters : x : Integer value to be converted y : Integer whose sign is required Returns : float value consisting of magnitude
1 min read
Python | math.ceil() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.ceil() function returns the smallest integral value greater than the number. If number is already integer, same number is returned. Syntax: math.ceil(x) Parameter: x: This is a numeric expression. Returns: Smallest integer no
1 min read
Python | math.factorial() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.factorial() function returns the factorial of desired number. Syntax: math.factorial(x) Parameter: x: This is a numeric expression. Returns: factorial of desired number. Time Complexity: O(n) where n is the input number. Auxi
2 min read
Python | math.copysign() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.copysign(a, b) function return a float with the magnitude (absolute value) of a but the sign of b. Syntax: math.copysign(a, b) Parameter: a, b: numeric values. Returns: Return absolute value of a but the sign of b. Code #1: #
1 min read
Python | math.gcd() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.gcd() function compute the greatest common divisor of 2 numbers mentioned in its arguments. Syntax: math.gcd(x, y) Parameter: x : Non-negative integer whose gcd has to be computed. y : Non-negative integer whose gcd has to be
2 min read
Python | math.cos() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.cos() function returns the cosine of value passed as argument. The value passed in this function should be in radians. Syntax: math.cos(x) Parameter: x : value to be passed to cos() Returns: Returns the cosine of value passed
2 min read
Practice Tags :
three90RightbarBannerImg