![]() |
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
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
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
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
Output : 0.05999999999999993 |
Reffered: https://www.geeksforgeeks.org
Python |
Related |
---|
![]() |
![]() |
![]() |
|
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |