Python math.modf() Method

Python math.modf() Method

  1. Syntax
  2. Example 1: Float and Integer Values as Input
  3. Example 2: Input Is Not a Numeric Value
  4. Example 3: modf() Over a List of Elements
  5. Example 4: modf() Over a Tuple of Elements
  6. Example 5: Multiply Fractional and Integral Parts of Two Numbers

The Python programming language is a dynamically-typed general purpose language. It is backed by various built-in and third-party modules, making it an ideal choice for many use cases.

This language offers a module, namely, math, that contains implementations for various mathematical operations.

Floating point numbers contain two parts - the integer part and the fractional part. The math module offers a method, namely, modf(), that can separate a floating point number into respective components.

This article will discuss the modf() method and some relevant examples.

Syntax

math.modf(x)

Parameters

Type Description
x Integer or Float An integer or float real number.

Return

The modf() method returns a tuple of two float values where the first element is the floating part and the second element is the integer part. Both the values have the same sign as the original or input number.

Example 1: Float and Integer Values as Input

import math

print(math.modf(1))
print(math.modf(0.0))
print(math.modf(-0.0))
print(math.modf(0.00001))
print(math.modf(323.132))
print(math.modf(995.0))
print(math.modf(-0.2124))
print(math.modf(-990.562))

Output:

(0.0, 1.0)
(0.0, 0.0)
(-0.0, -0.0)
(1e-05, 0.0)
(0.132000000000005, 323.0)
(0.0, 995.0)
(-0.2124, -0.0)
(-0.5620000000000118, -990.0)

The Python code above uses the modf() method to split a few numbers. The numbers are chosen so that all the possible corner cases are covered.

Note that when an integer is given as an input, the fractional part defaults to 0.0. Moreover, the sign of the input number is replicated to the elements of the output tuple.

Example 2: Input Is Not a Numeric Value

import math

print(math.modf('THIS IS A STRING'))

Output:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(math.modf('THIS IS A STRING'))
TypeError: must be real number, not str

The modf() method only accepts floating-point and integer values. Since a string input was given to this method, it raised a TypeError exception which says that the input must be a real number.

Example 3: modf() Over a List of Elements

import math

nums = [1, 2.342, 44.2, -123.5998, 0., .0, 0.0]

for i in range(len(nums)):
    print(math.modf(nums[i]))

Output:

(0.0, 1.0)
(0.3420000000000001, 2.0)
(0.20000000000000284, 44.0)
(-0.5998000000000019, -123.0)
(0.0, 0.0)
(0.0, 0.0)
(0.0, 0.0)

The Python code above first defines a list of floating-point and integral numbers. Next, it iterates over them using a for loop and for each element, it calls the modf() method, which splits the number into its fractional and integral parts.

Example 4: modf() Over a Tuple of Elements

import math

nums = (1, 2.342, 44.2, -123.5998, 0., .0, 0.0)

for i in range(len(nums)):
    print(math.modf(nums[i]))

Output:

(0.0, 1.0)
(0.3420000000000001, 2.0)
(0.20000000000000284, 44.0)
(-0.5998000000000019, -123.0)
(0.0, 0.0)
(0.0, 0.0)
(0.0, 0.0)

The Python code above first defines a tuple of floating-point and integral values. Next, it iterates over them using a for loop and for each element, it calls the modf() function, which further splits the number into its fractional and integral components.

Example 5: Multiply Fractional and Integral Parts of Two Numbers

import math

a = 4.2
b = 8.8
fa, ia = math.modf(a)
fb, ib = math.modf(b)
print("Product of Fractional Parts:", fa * fb)
print("Product of Integral Parts", ia * ib)

Output:

Product of Fractional Parts: 0.16000000000000028
Product of Integral Parts 32.0

The Python program above finds fractional and integral parts of two numbers. Next, it prints the product of integral parts and fractional parts.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

LinkedIn GitHub

Related Article - Python Math

  • Python math.pow() Method
  • Python Math.erf() Method
  • Python Math.erfc() Method
  • Python Math.expm1() Method
  • Python Math.fabs() Method
  • Python Math.factorial() Method