Friday, May 15, 2026
HomeLanguagesPython | ldexp() function

Python | ldexp() function

ldexp() function is one of the Standard math library function in Python, which returns x * (2**i). This is also called as inverse of Python frexp() function.

Syntax: math.ldexp(x, i) 

Parameters: x : Any valid number (+ve or -ve) i : Any valid number (+ve or -ve) 

Returns : Returns the value of x * (2**i).

Time Complexity: O(1)

Auxiliary Space: O(1)

For example, if x = 3 and i = 4 then, Math.ldexp(3, 4) = 3*16 = 48. 

Code #1: 

Python3




# Python3 code demonstrate ldexp() function
 
# importing math library
import math
 
# ldexp() Function on +ve nd -ve Numbers
print(math.ldexp(9, 3))
print(math.ldexp(-5, 2))
 
# ldexp() Function on fractional Number
print(math.ldexp(3.5, 2))
print('%.2f' %math.ldexp(-6.8, 3))


Output:

72.0
-20.0
14.0
-54.40

  Code #2: 

Python3




# Python3 code demonstrate ldexp() function
 
# importing math library
import math
 
# Tuple Declaration
tpl = (9, -5, 3.5, -6.8)
 
# List Declaration
lst = [13, 4, 8.4, -6.7]
 
# ldexp() Function on +ve nd -ve Numbers
print(math.ldexp(tpl[0], 3))
print(math.ldexp(tpl[3], 2))
 
# ldexp() Function on fractional Number
print(math.ldexp(lst[1], 2))
print('%.2f' %math.ldexp(lst[2], 3))


Output:

72.0
-27.2
16.0
67.20

  Code #3: If the X value or i value argument is not a number, ldexp() function will return TypeError

Python3




# Python3 code demonstrates when error occurs
 
# importing the math library
import math
 
# string value taken
print(math.ldexp('25', 5))
print(math.ldexp(25, '5'))


Output:

TypeError: a float is required
TypeError: a float is required
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS