Computer >> Computer tutorials >  >> Programming >> Python

Python frexp() Function


This function is used to find the mantissa and exponent of a number. It is heavily used in mathematical calculations. In this article we will see the various ways it can be used in python programs.

Syntax

Below is the syntax and its description for using this function.

math.frexp( x )
Parameters: Any valid number (positive or negative).
Returns: Returns mantissa and exponent as a pair (m, e) value of a given number x.
Exception: If x is not a number, function will return TypeError

Simple Expressions

Below is an example program where the function is applied directly on given numbers to give us mantissa and exponent.

Example

import math
# Getting mantissa and exponent
print(math.frexp(12))
print(math.frexp(10.5))

Output

Running the above code gives us the following result −

(0.75, 4)
(0.65625, 4)

With lists and tuples

In this example we take a list and a tuple and apply the function for specific elements using index of the sequence.

Example

import math
listA = [3,9,4,7]
tupA = (3.8,12.6,12.5)
# Getting mantissa and exponent
print(math.frexp(listA[2]))
print(math.frexp(tupA[1]))

Output

Running the above code gives us the following result −

(0.5, 3)
(0.7875, 4)