Python math.frexp() Method

The math.frexp() method is used to break down a number into its mantissa and exponent as a pair (m,e).

Syntax

import math
math.frexp(num)

Parameter

num(required): It is a number.

Return Value

Returns mantissa and exponent of num as the pair (m, e), where m is the float, and e is an integer.

However, if the given value is not a number, it returns a TypeError.

Visual Representation

Visual Representation of Python math.frexp() Method

Example 1: How to Use math.frexp() Method

# Importing math library
import math

print(math.frexp(4)) #positive number
print(math.frexp(7.3)) #floating number
print(math.frexp(-4)) #negative number
print(math.frexp(-7.3))

Output

(0.5, 3)
(0.9125, 3)
(-0.5, 3)
(-0.9125, 3)

Example 2: Passing a list and tuple

import math

# creating a list
list = [11, 21.11, 21.19, 30, 7]

# creating a tuple
tuple = (-15.31, -41.31, -11.21, 46.19, 7.6)

print("3rd and 5th elements in list : ")

print(math.frexp(list[2]))
print(math.frexp(list[4]))

print("2nd and 4th elements in tuple : ")

print(math.frexp(tuple[1]))
print(math.frexp(tuple[3]))

Output

3rd and 5th elements in list : 
(0.6621875, 5)
(0.875, 3)
2nd and 4th elements in tuple : 
(-0.64546875, 6)
(0.72171875, 6)

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.