Python math.frexp() Method
The Python math.frexp() method is used to decompose a floating-point number into its normalized fraction and exponent.
- Given a floating-point number x, the math.frexp(x) method returns a tuple "(m, e)" where "m" is the normalized fraction (also known as the significand or mantissa) and "e" is the exponent.
- The normalized fraction "m" is a float representing the fractional part of the number x which is greater than or equal to 0.5 and less than 1.0.
- The exponent "e" is an integer representing the power of 2 by which the normalized fraction is multiplied to get the original number x.
Mathematically, for a non-zero floating-point number x, it can be represented as −
x = m × 2e
Where, "m" is the normalized fraction and "e" is the exponent.
Syntax
Following is the basic syntax of the Python math.frexp() method −
math.frexp(x)
Parameters
This method accepts a numeric value as a parameter representing the floating-point number that you want to decompose.
Return Value
The method returns a tuple "(m, e)", where "m" is a float representing the normalized fraction and "e" is an integer representing the exponent.
The fraction "m" satisfies the condition "0.5 <= abs(m) < 1.0", and "x" is approximately equal to "m * 2**e". If "x" is zero, both "m" and "e" are zero. If "x" is a NaN or infinite, both "m" and "e" are NaN or infinite.
Example 1
In the following example, we are calculating the mantissa and exponent of the floating-point number 10 using the frexp() method −
import math
mantissa, exponent = math.frexp(10)
print("The result obtained is:",mantissa, exponent)
Output
The output obtained is as follows −
The result obtained is: 0.625 4
Example 2
Here, we are calculating the mantissa and exponent of the negative floating-point number "-5" −
import math
mantissa, exponent = math.frexp(-5)
print("The result obtained is:",mantissa, exponent)
Output
Following is the output of the above code −
The result obtained is: -0.625 3
Example 3
Now, we calculate the mantissa and exponent of a fractional number using the frexp() method −
import math
mantissa, exponent = math.frexp(0.75)
print("The result obtained is:",mantissa, exponent)
Output
We get the output as shown below −
The result obtained is: 0.75 0
Example 4
In this example, we calculate the mantissa and exponent of the floating-point number 0. Since 0 has no magnitude, both the mantissa and exponent are 0 −
import math
mantissa, exponent = math.frexp(0)
print("The result obtained is:",mantissa, exponent)
Output
The result produced is as shown below −
The result obtained is: 0.0 0
python_maths.htm