AppDividend
Latest Code Tutorials

Python frexp() Function Example

0

Python frexp() is an inbuilt function under the math library that helps us to find mantissa and exponent of x as the pair (m, e), where m is the float, and e is the integer such that x == m * 2**e. If the value of x is 0 then this function returns (0.0,0), otherwise it returns 5 <= abs(m) <1.

Python frexp()

The frexp() function is one of the Standard math Library functions in Python. It returns mantissa and exponent as a pair (m, e) of a given value x, where mantissa is a floating-point number, and exponent is an integer value. m is the float, and e is an integer such that x == m * 2**e exactly.

If the x is zero, returns (0.0, 0), otherwise 0.5 <= abs(m) < 1. This is used to “pick apart” the internal representation of the float in a portable way.

Syntax

math.frexp(x)

Here x is a number for which we will find mantissa and exponent.

Return Value

The frexp() function returns mantissa and exponent of x as the pair (m, e), where m is the float, and e is an integer. However, if the given value x is not a number, then this function returns a TypeError.

Programming Example

See the following code.

# app.py

# Importing math library
import math

# Demonstrating working of frexp()
# Using different types of value of x

# When x is positive number
x = 5
print("Pair of mantissa and exponent of ", x, " is: ", math.frexp(x))

# When x is float type number
x = 6.4
print("Pair of mantissa and exponent of ", x, " is: ", math.frexp(x))

# When x is a negative number
x = -32
print("Pair of mantissa and exponent of ", x, " is: ", math.frexp(x))

# Declaring a list
x = [4, 3, 7]

# Using frexp() with the 3rd value of the list
print("Pair of mantissa and exponent of ", x[2], " is: ", math.frexp(x[2]))

# When x is not a number
x = '41'
print("Pair of mantissa and exponent of ", x, " is: ", math.frexp(x))

Output

Pair of mantissa and exponent of  5  is:  (0.625, 3)
Pair of mantissa and exponent of  6.4  is:  (0.8, 3)
Pair of mantissa and exponent of  -32  is:  (-0.5, 6)
Pair of mantissa and exponent of  7  is:  (0.875, 3)
Traceback (most recent call last):
  File "frexp.py", line 27, in <module>
	print("Pair of mantissa and exponent of ",x," is: ",math.frexp(x))
TypeError: must be real number, not str

In the above code, we have taken different types of values of x and checked the output using the frexp() method. We can see that in each case, an output is in (m,e) pair. 

At last, when we have declared value of x as a character, a TypeError is returned.

Use frexp() with Python tuple and list

See the following code in which we have defined Python list and tuple.

# app.py

import math

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

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

# calculating mantissa and exponent
# of 1st, 3rd elements in list
print(math.frexp(lst[0]))
print(math.frexp(lst[2]))

# calculating mantissa and exponent
# of 2nd, 3rd and 4th elements in tuple
print(math.frexp(tpl[1]))
print(math.frexp(tpl[2]))
print(math.frexp(tpl[3]))

Output

python3 app.py
(0.6875, 4)
(0.6621875, 5)
(-0.64546875, 6)
(-0.700625, 4)
(0.72171875, 6)

Conclusion

Python frexp() method is one of the Python Mathematical Functions that is used to return the mantissa and exponent of x, as a pair (m, e) where m is the float value and e is an integer value.

See also

Python fmod()

Python factorial

Python math.fabs()

Python math copysign()

Python math functions

Python math.sqrt()

Python math.floor()

Leave A Reply

Your email address will not be published.

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