Python frexp

The Python frexp math function is used to return the mantissa and exponent of x, as pair (m, e). Where m is a float value, and e is an integer value. In this section, we discuss how to use frexp function in Python Programming language with an example.

The syntax of the frexp Function is

math.frexp(Number);
  • If the number argument is a positive or negative number, it returns the mantissa and exponent of x as pair (m, e).
  • If the number argument is not a number, the frexp function returns TypeError.

TIP: By making the reverse approach, we can easily find the Number value, and the formula behind this is: Number = m * 2**e. Here, m is the first argument, and e is the second argument.

Python frexp Function Example

The Python frexp function returns the mantissa and exponent of x as pair (m, e). In this example, we will return the frexp value of different data types and display the output.

import math

Tup = (10.98, 20.26, -30.05, -40.95 , 50.85) # Tuple Declaration
Lis = [-10.98, 32.65, -39.29, -42.15 , 39.97] # List Declaration

print('FREXP() Function on Positive Number = ', math.frexp(2))
print('FREXP() Function on Negative Decimal = ', math.frexp(-3))

print('FREXP() Function on Positive Decimal = ', math.frexp(4.5))
print('FREXP() Function on Negative Decimal = ', math.frexp(-6.5))

print('FREXP() Function on Tuple Item = ', math.frexp(Tup[2]))
print('FREXP() Function on Tuple Item = ', math.frexp(Tup[4]))
print('FREXP() Function on List Item = ', math.frexp(Lis[2]))
print('FREXP() Function on List Item = ', math.frexp(Lis[4]))

print('FREXP() Function on Multiple Number = ', math.frexp(10 + 20 - 40.6578))

print('FREXP() Function on String Value = ', math.frexp('2.95'))
Python FREXP Function

In the beginning, We declared List and Tuple with some random Python values.

Within the first two statements, We used the Python frexp Function directly on both the Positive and Negative Numbers.

Here, math.frexp(2) is returning output as (0.5, 2). It means m = 0.5 and e = 2. Let us make the reverse approach so that you can understand this Math function well.

As we said before, Number = m * 2**e (2 power e). Here, we have given the Number argument as 2

Number = 0.5 * 2**2 => 0.5 * 4 = 2