C ldexp() Function

Definition
In C, ldexp() returns x multiplied by 2 raised to exp. ldexp(1.5, 3) is 12, because 1.5 × 2³ is 12.
You pass a double and an int power. Both are required. The result is a double. Include <math.h>.
Syntax
Pass the number, then the integer power of two. Start with this call.
| Call | What it does |
|---|
ldexp(1.5, 3) | Returns 12, which is 1.5 × 8. |
Include <math.h>. On GCC, link with -lm. exp is an int, not a double.
Library declarations
double ldexp(double x, int exp);
float ldexpf(float x, int exp);
long double ldexpl(long double x, int exp);
Parameters
| Parameter | Description |
|---|
| x | Required. The double to scale. |
| exp | Required. The int power of two. It can be negative. |
Return Value
| Return Value | Description |
|---|
| Scaled double | x × 2^exp. |
C Examples
Example 1 — Scale Up by a Power of Two
Multiply 5.25 by 2³.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", ldexp(5.25, 3));
return 0;
}
How It Works
- Include math.h. That declares
ldexp. - Exponent 3. Means multiply by 8.
- Compute.
5.25 × 8 = 42. - Print as double.
%.1f shows one decimal place.
Example 2 — Negative Exponent (Divide)
A negative exp divides by a power of two.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", ldexp(8.0, -2));
printf("%.1f\n", ldexp(8.0, -1));
return 0;
}
How It Works
- exp = -2. Divide by
2² = 4 → 8 / 4 = 2. - exp = -1. Divide by 2 →
4.0. - Same API both ways. Sign of
exp chooses multiply vs divide. - Handy. Halving/doubling floats without writing
/ 2.0 loops by hand.
Example 3 — Round-Trip with frexp
Split with frexp, then rebuild with ldexp.
#include <stdio.h>
#include <math.h>
int main(void) {
int exp;
double x = 42.0;
double frac = frexp(x, &exp);
double back = ldexp(frac, exp);
printf("frac=%.6f exp=%d\n", frac, exp);
printf("back=%.1f\n", back);
return 0;
}
frac=0.656250 exp=6
back=42.0
How It Works
- Split.
frexp(42, &exp) yields mantissa ≈ 0.65625 and exp = 6. - Rebuild.
ldexp(frac, 6) multiplies by 2⁶ = 64. - Match. The result is the original
42.0. - Pair them. Use this pattern whenever you edit the exponent separately.
Frequently Asked Questions
ldexp() returns x multiplied by 2 raised to exp. ldexp(1.5, 3) is 12.
Both are required. x is the double to scale. exp is an int power of two.
A double equal to x times 2 to the power exp.
Include <math.h>. On GCC, link with -lm.
No. The power is an int.
Next: log()
Compute the natural logarithm of a positive floating value.
log() tutorial →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful