ldexp()

ldexp() returns the mantissa and exponent of an input number.
ldexp(m,e) # output n 
n : is the output number
m : is the mantissa , a floating point number
e : is the exponent , an integer

Relation between output number with Mantissa ( m ) and exponent ( e )
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

Understanding math.ldexp() and Related Functions

Use Case: Scientific Computing

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.

Example: Comparison with frexp()

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

Binary-Decimal Conversion Example

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.


We can also use frexp() the inverse function of ldexp() to get Mantissa and exponent of n
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer