What is math.ldexp(x, i) in Python?
The math.ldexp() method is used to compute the expression x * (2**i).
ldexp() is the inverse function of frexp().
The
frexp()method takes one argument and returnsmantissam and exponent e as a tuple(m,e).
Syntax
math.ldexp(x, i)
Parameters
ldexp takes two signed numbers as parameters:
x(float value): the first argument is basically mantissam.i(integer value): the second argument is basically exponente.
To understand this concept, learn about the
frexp()method first.
Return value
This method calculates and returns the value of this expression: x * (2**i).
Example
In the code snippet below, we pass two arguments to the math.ldexp() method, which returns the result of x * (2**i) on different argument values.
# Load maths moduleimport mathx=2i=4# demo examplesprint(math.ldexp(x, i))print(math.ldexp(8, 4))print(math.ldexp(12, -3))print(math.ldexp(-7, 2))