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 example.

The syntax of the frexp Function in Python Programming Language is

math.frexp(Number);

Number: It can be a number or a valid numerical expression.

  • If the number argument is a positive or negative number, the Python frexp function returns the mantissa and exponent of x, as pair (m, e).
  • If the number argument is not a number, the Python frexp function return 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 are going to return the frexp value of different data types and display the output.

# Python frexp Function

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 Python List and Tuple with some random Python values.

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

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

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

From the above screenshot, you can observe that 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 were given the Number argument as 2

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

Within this Python frexp function example,

  • Next two statements, We used the frexp Function directly on both the Positive decimals and negative decimals.
  • Next four statements, We used the frexp Function on Tuple and List items. If you observe the above screenshot, the frexp function is working perfectly on them.
  • Next statement, We tried frexp Function directly on multiple values.
  • Last, We tried frexp Function on the String value, and frexp returns TypeError as output.