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 returns mantissa m 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 mantissa m.
  • i (integer value): the second argument is basically exponent e.

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 module
import math
x=2
i=4
# demo examples
print(math.ldexp(x, i))
print(math.ldexp(8, 4))
print(math.ldexp(12, -3))
print(math.ldexp(-7, 2))

Free Resources