ldexp(m,e) # output n
n : is the output number n == m * 2**e
Example
import math
print(math.ldexp(2,3)) # 16.0
With negative numbers
print(math.ldexp(-2,3)) # -16.0
print(math.ldexp(2,-3)) # 0.25
The second argument ( exponent ) has to be an integer, we can't use float.
print(math.ldexp(2.2,-3.3))
This will generate an TypeError
math.ldexp() is useful in scenarios involving floating-point arithmetic, particularly in scientific computing and graphics, where low-level operations require manipulating the mantissa and exponent of a number.
Using frexp() to decompose a number and ldexp() to reconstruct it:
import math
n = 16
m, e = math.frexp(n) # (mantissa, exponent)
print(m, e) # Outputs: 0.5, 5
print(math.ldexp(m, e)) # Outputs: 16.0
mantissa = 0.75
exponent = 4
print(math.ldexp(mantissa, exponent)) # Outputs: 12.0
This is useful in binary-decimal conversions for precise floating-point calculations.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.