Python math.ldexp() Method

Python math.ldexp() Method

  1. Syntax
  2. Parameters
  3. Returns
  4. Example Codes

Python math.ldexp() method is an efficient way of converting the mantissa and exponent of any number to the original number. The method returns x * (2**i).

Syntax

math.frexp(x,i)

Parameters

x Any number. Either positive or negative.
i Any number. Either positive or negative.

Returns

This method’s return type is a floating point value representing x * (2**i).

Example Codes

Let’s use various examples below to learn the math.Idexp() method.

Use of the math.ldexp() Method

Example Code:

import math

x=-30.9
i=2
value=math.ldexp(x,i)
print(f"The number is {value}.")

x=0
i=0
value=math.ldexp(x,i)
print(f"The number is {value}.")

x=3
i=-8
value=math.ldexp(x,i)
print(f"The number is {value}.")

x=math.inf
i=1
value=math.ldexp(x,i)
print(f"The number is {value}.")

Output:

The number is -123.6.
The number is 0.0.
The number is 0.01171875.
The number is inf.

Note that the values may be either positive or negative. These methods are used in mathematical computations related to geometry and have a specific application in astronomical calculations.

Error While Using the math.ldexp() Method

Example Code:

import math

##entering a string
x="Hi"
i=1
value=math.ldexp(x,i)
print(f"The number is {value}.")

##entering a list
x=[1,2,3]
i=9
value=math.ldexp(x,i)
print(f"The number is {value}.")

##entering complex numbers
x=1+5j
i=9+4j
value=math.ldexp(x,i)
print(f"The number is {value}.")

Output:

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    value=math.frexp(x)
TypeError: must be a real number, not str
Traceback (most recent call last):
  File "main.py", line 19, in <module>
    value=math.frexp(x)
TypeError: must be a real number, not a list
Traceback (most recent call last):
  File "main.py", line 28, in <module>
    value=math.frexp(x)
TypeError: can't convert complex to float

Note that the above-stated errors will be raised when the argument values are out of data range.

Use Multiple Entries on the math.ldexp() Method

Example Code:

import math

lists = [15, -13.76, 21]
i= [0,1,2]

print(f"The number is {math.ldexp(lists[0],i[0])}.")
print(f"The number is {math.ldexp(lists[1],i[1])}.")
print(f"The number is {math.ldexp(lists[2],i[2])}.")

Output:

The number is 15.0.
The number is -27.52.
The number is 84.0.

Note that you can find the value of any number using the mantissa and exponent method through the array notation.

the math.ldexp() Method and Its Inverse

Example Code:

import math

x=-30.9
value=math.frexp(x)
print(f"The mantissa and exponent of {x} are {value}.")

m=value[0]
e=value[1]
inverse=math.ldexp(m, e)
print(f"The inverse is {inverse}.")

Output:

The mantissa and exponent of -30.9 are (-0.965625, 5).
The inverse is -30.9.

Note that math.ldexp() is the inverse of the method math.frexp().

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python Math

  • Python math.pow() Method
  • Python Math.erf() Method
  • Python Math.erfc() Method
  • Python Math.expm1() Method
  • Python Math.fabs() Method
  • Python Math.factorial() Method