Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of ldexp?

Tags:

c++

I was wondering what people use the function ldexp() for in practical applications.

This is the description:

Returns the result of multiplying x (the significand) by 2 raised to the power of exp (the exponent).

like image 956
BlueTrin Avatar asked Jun 13 '13 13:06

BlueTrin


People also ask

What is Ldexp function?

The ldexp() function returns the value of x*(2exp). If an overflow results, the function returns +HUGE_VAL for a large result or -HUGE_VAL for a small result, and sets errno to ERANGE.

What is Ldexp in Python?

ldexp() method returns x * (2**i) of the given numbers x and i, which is the inverse of math.


2 Answers

ldexp and its dual, frexp, deal with the mantissa and exponent of a floating-point number. They provide a way to get at the internal representation without doing direct bit manipulation.

like image 62
Michael Kristofik Avatar answered Oct 03 '22 20:10

Michael Kristofik


When used with frexp , the ldexp function is useful in situations that require repeated multiplication by 2. If the next multiplication causes an overflow or underflow, use frexp to separate the mantissa from the exponent. This gives you complete control over the exponent and the mantissa, so you can operate on them separately without any loss of precision. When you are finished, use ldexp to combine the mantissa and exponent again.

See more details here.

like image 28
Curious Avatar answered Oct 03 '22 19:10

Curious