C Math ldexp() Function
Example
Multiply numbers by a power of 2:
printf("%f\n", ldexp(3.0, 2));
printf("%f\n", ldexp(0.5, 3));
printf("%f", 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 <math.h> header file.
Tip: frexp() does the opposite: it splits a number into a fraction and a power of 2.
Syntax
double ldexp(double x, int y);
There are also versions for float and long double values: ldexpf() and ldexpl().
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 C Math Tutorial.