C Math ldexp() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

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.

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).

📝 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

⚡ Quick Reference

CallMeaningResult
ldexp(5.25, 3)5.25 × 842.0
ldexp(8.0, -2)8 ÷ 42.0
ldexp(0.5, 4)0.5 × 168.0
ldexp(m, e)After frexpRebuild x
x * pow(2, e)ManualUse 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

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;
}

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

ldexp(8, -2) divides 8 by 4.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    printf("ldexp(8,  2) = %.1f  (8 * 4)\n", ldexp(8.0,  2));
    printf("ldexp(8, -2) = %.1f  (8 / 4)\n", ldexp(8.0, -2));
    printf("ldexp(8,  0) = %.1f  (unchanged)\n", ldexp(8.0,  0));

    return 0;
}

How It Works

exp = 0 leaves x unchanged (multiply by 2&sup0; = 1). Negative exponents divide instead of multiply.

📈 Practical Patterns

Rebuild from frexp, halve values, and compare with pow.

Example 3 — Reconstruct a Value After frexp()

Split with frexp, rebuild with ldexp.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double original = 123.45;
    int exp;
    double mantissa = frexp(original, &exp);
    double rebuilt = ldexp(mantissa, exp);

    printf("original = %.2f\n", original);
    printf("mantissa = %.6f, exp = %d\n", mantissa, exp);
    printf("rebuilt  = %.2f\n", rebuilt);

    return 0;
}

How It Works

frexp and ldexp are inverse operations. This round-trip always recovers the original value (within floating-point precision).

Example 4 — Halve a Value with ldexp(x, -1)

A common pattern for binary scaling down.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double brightness = 1.0;
    size_t i;

    printf("Start: %.4f\n", brightness);
    for (i = 0; i < 3; i++) {
        brightness = ldexp(brightness, -1);
        printf("After halve %zu: %.4f\n", i + 1, brightness);
    }

    return 0;
}

How It Works

Each ldexp(x, -1) divides by 2. Useful for fading effects or successive averaging in graphics.

Example 5 — ldexp() vs pow(2, exp) * x

Both compute the same math; ldexp is the idiomatic choice for base 2.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = 5.25;
    int exp = 3;
    double via_ldexp = ldexp(x, exp);
    double via_pow = x * pow(2.0, exp);

    printf("ldexp(x, %d)     = %.6f\n", exp, via_ldexp);
    printf("x * pow(2, %d)   = %.6f\n", exp, via_pow);

    return 0;
}

How It Works

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.

🚀 Common Use Cases

  • Reconstruct floats — after frexp mantissa/exponent split.
  • Fast multiply/divide by 2ldexp(x, n) or ldexp(x, -n).
  • Graphics — brightness halving, mip-map level scaling.
  • Audio — gain changes in powers of two.
  • 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.

📝 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.

⚡ 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.

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.

💡 Best Practices

✅ Do

  • Use ldexp(x, n) for multiply/divide by 2n
  • Rebuild after frexp with ldexp(m, e)
  • Use ldexpf / ldexpl for matching types
  • Check isfinite() for extreme exponents
  • Include <math.h> and link -lm

❌ Don’t

  • Use pow(2, exp) * x when ldexp fits
  • Confuse ldexp with frexp (opposite jobs)
  • Expect decimal scaling (that is x * pow(10, n))
  • Ignore overflow with huge positive exp
  • Pass a float exponent—exp must be int

Key Takeaways

Knowledge Unlocked

Five things to remember about ldexp()

Power-of-two scaling in C, explained simply.

5
Core concepts
📚 02

+exp up

−exp down.

Sign
📈 03

frexp

Inverse.

Pair
📄 04

42

5.25, exp 3.

Example
🔄 05

vs pow

Prefer ldexp.

Tip

❓ Frequently Asked Questions

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.

Explore C Math Functions

Continue with log() or browse the full math function reference.

Math Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful