Python math.ldexp() Method
The Python math.ldexp() method is used to calculate the value of a number "x" multiplied by 2i, where "x" is the significand (also known as mantissa) and "i" is the exponent.
For example, if you have a significand "x = 0.75" and an exponent "i = 3", then math.ldexp(0.75, 3) will return 0.75 × 23 = 6.0.
Note: To use this function, you need to import math module.
Syntax
Following is the basic syntax of the Python math.ldexp() method −
math.ldexp(x, i)
Parameters
This method accepts the following parameters −
x − It is the significand, which is a real number between 0.5 (inclusive) and 1.0 (exclusive).
i − It is the exponent, which is an integer representing the power of 2.
Return Value
The method returns a float, which is the result of x × 2exp.
Example 1
In the following example, we are calculating the ldexp of a number by raising it to a positive exponent −
import math
result = math.ldexp(0.75, 3)
print("The result obtained is:",result)
Output
The output obtained is as follows −
The result obtained is: 6.0
Example 2
Here, we are calculating the ldexp of a number by raising it to a negative exponent −
import math
result = math.ldexp(1.5, -2)
print("The result obtained is:",result)
Output
Following is the output of the above code −
The result obtained is: 0.375
Example 3
Now, we are calculating the ldexp of a number using "0" as the significand −
import math
result = math.ldexp(0, 5)
print("The result is:",result)
Output
We get the output as shown below −
The result obtained is: 0.0
Example 4
In this example, we use variables "significand" and "exponent" to store the values "1.25" and "-3" respectively. We then calculate the ldexp −
import math
significand = 1.25
exponent = -3
result = math.ldexp(significand, exponent)
print("The result obtained is:",result)
Output
The result produced is as shown below −
The result obtained is: 0.15625