The ldexp() function computes x × 2exp—scale a number up or down by powers of two. It is the inverse of frexp() and a fast alternative to pow(2, exp) * x in graphics and numeric code.
01
x·2e
Scale.
02
math.h
Link -lm.
03
+exp
Multiply.
04
−exp
Divide.
05
frexp
Inverse.
06
vs pow
Prefer ldexp.
Fundamentals
Definition and Usage
ldexp(x, exp) multiplies x by 2 raised to exp. Positive exp scales up (like shifting the binary point left); negative exp scales down.
Example from the reference: ldexp(5.25, 3) = 5.25 × 2³ = 5.25 × 8 = 42. If frexp splits a number apart, ldexp puts mantissa and exponent back together.
💡
Beginner Tip
Need to double a value three times? ldexp(x, 3) is clearer and often faster than x * 8 or x * pow(2, 3). The name means “load exponent”—pair it with frexp (extract exponent).
Foundation
📝 Syntax
Standard C declaration:
C
double ldexp(double x, int exp);
Related variants
C
float ldexpf(float x, int exp); /* <math.h> */
long double ldexpl(long double x, int exp); /* <math.h> */
Parameters
x — the mantissa or base value to scale.
exp — integer exponent for the power of two.
Return Value
x × 2exp as a double.
ldexp(5.25, 3) returns 42.0.
ldexp(0, exp) returns 0 for any exp.
Headers and linking
#include <math.h>
Compile: gcc ldexp.c -std=c11 -o ldexp -lm
Cheat Sheet
⚡ Quick Reference
Call
Meaning
Result
ldexp(5.25, 3)
5.25 × 8
42.0
ldexp(8.0, -2)
8 ÷ 4
2.0
ldexp(0.5, 4)
0.5 × 16
8.0
ldexp(m, e)
After frexp
Rebuild x
x * pow(2, e)
Manual
Use ldexp instead
Scale
ldexp(x, exp)
x * 2^exp
Split
frexp(x, &e)
Inverse
Double
ldexp(x, 1)
x * 2
Halve
ldexp(x, -1)
x / 2
Hands-On
Examples Gallery
Compile every example with gcc file.c -std=c11 -o out -lm. Power-of-two scaling shows up in float reconstruction and fast multiply/divide by 2.
📚 Getting Started
Multiply a value by a power of two.
Example 1 — Scale 5.25 by 2³
Compute 5.25 × 8 with ldexp.
C
#include <math.h>
#include <stdio.h>
int main(void) {
double x = 5.25;
int exponent = 3;
double result = ldexp(x, exponent);
printf("x = %.2f, exp = %d\n", x, exponent);
printf("ldexp(x, exp) = %.2f\n", result);
return 0;
}
📤 Output:
x = 5.25, exp = 3
ldexp(x, exp) = 42.00
How It Works
2³ is 8. Multiplying 5.25 by 8 gives 42—the same result as the reference example.
Example 2 — Negative Exponent Divides by Powers of Two
Results match for this case. Prefer ldexp when scaling by powers of two—it avoids a general pow call and is often implemented as a direct exponent adjustment.
Applications
🚀 Common Use Cases
Reconstruct floats — after frexp mantissa/exponent split.
Fast multiply/divide by 2 — ldexp(x, n) or ldexp(x, -n).
Custom float math — build values from mantissa and exponent parts.
🧠 How ldexp() Works
1
Receive x and exp
Base value and integer power-of-two exponent.
Input
2
Adjust exponent bits
Often implemented by modifying the float’s exponent field directly.
Compute
3
Return x × 2exp
Scaled double (or overflow/underflow if extreme).
Output
=
🔢
Scaled value
Inverse of frexp—rebuild from parts.
Important
📝 Notes
Computes x × 2exp, not x × 10exp.
ldexp(0, exp) is 0 for any exp.
exp = 0 returns x unchanged.
Large exp may overflow; very negative exp may underflow to 0.
Inverse of frexp() on the decomposed parts.
Link with -lm on GCC/Clang Unix-like builds.
Performance
⚡ Optimization
ldexp() is typically faster than pow(2, exp) * x because it can adjust the IEEE-754 exponent directly. For integer powers of two, always prefer ldexp over general exponentiation.
Wrap Up
Conclusion
ldexp() multiplies by powers of two—scale up with positive exp, scale down with negative. It rebuilds values from frexp() parts and replaces pow(2, n) * x in idiomatic C.
Continue with log() for natural logarithms, or review frexp() as the splitting counterpart.
ldexp(x, exp) returns x multiplied by 2 raised to the power exp — that is, x * 2^exp. For example, ldexp(5.25, 3) is 42.0 because 5.25 * 8 = 42.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double ldexp(double x, int exp).
ldexp builds a value: mantissa * 2^exponent. frexp splits a value into mantissa and exponent. They are inverses: ldexp(frexp(x, &e), e) equals x.
Yes. A negative exponent divides by powers of two. ldexp(8.0, -2) is 2.0 because 8 / 4 = 2. ldexp(x, -1) is x / 2.
Prefer ldexp(x, exp) for scaling by powers of two. It is typically faster and avoids computing 2^exp separately. Use pow only when the base is not 2.
Large positive exp may overflow to INFINITY. Large negative exp may underflow to 0.0. Check with isfinite() when exponents can be extreme.
Did you know?
The names frexp and ldexp come from Fortran-era math libraries: fraction extract and load exponent. Together they let you move between a float’s internal exponent form and its normal decimal appearance without bit-twiddling yourself.