C++ cmath ldexp() Function
Example
Multiply numbers by a power of 2:
cout << ldexp(3.0, 2) << "\n";
cout << ldexp(0.5, 3) << "\n";
cout << ldexp(5.0, -1);
Definition and Usage
The ldexp() function returns x multiplied by 2 to the power of y (x * 2y).
For example, ldexp(3.0, 2) is 3 * 2 * 2, which is 12. A negative y divides by 2 instead, so ldexp(5.0, -1) is 2.5.
The ldexp function is defined in the <cmath> header file.
Tip: frexp() does the opposite: it splits a number into a fraction and a power of 2.
Syntax
ldexp(double x, int y)
The function also works with float and long double values. Then it returns a value of the same type.
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. The number to multiply. |
| y | Required. An int with the power of 2 to multiply by. |
Technical Details
| Returns: | A double value with the result of x * 2y. |
|---|
Related Pages
Read more about math in our Math Tutorial.