C ldexp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

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.

C
ldexp(1.5, 3);
CallWhat 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
C
double ldexp(double x, int exp);
float ldexpf(float x, int exp);
long double ldexpl(long double x, int exp);

Parameters

ParameterDescription
xRequired. The double to scale.
expRequired. The int power of two. It can be negative.

Return Value

Return ValueDescription
Scaled doublex × 2^exp.

C Examples

Example 1 — Scale Up by a Power of Two

Multiply 5.25 by 2³.

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

int main(void) {
    printf("%.1f\n", ldexp(5.25, 3));
    return 0;
}

How It Works

  1. Include math.h. That declares ldexp.
  2. Exponent 3. Means multiply by 8.
  3. Compute. 5.25 × 8 = 42.
  4. Print as double. %.1f shows one decimal place.

Example 2 — Negative Exponent (Divide)

A negative exp divides by a power of two.

C
#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

  1. exp = -2. Divide by 2² = 4 → 8 / 4 = 2.
  2. exp = -1. Divide by 2 → 4.0.
  3. Same API both ways. Sign of exp chooses multiply vs divide.
  4. Handy. Halving/doubling floats without writing / 2.0 loops by hand.

Example 3 — Round-Trip with frexp

Split with frexp, then rebuild with ldexp.

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

How It Works

  1. Split. frexp(42, &exp) yields mantissa ≈ 0.65625 and exp = 6.
  2. Rebuild. ldexp(frac, 6) multiplies by 2⁶ = 64.
  3. Match. The result is the original 42.0.
  4. 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: lgamma()

Compute the natural logarithm of a positive floating value.

lgamma() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful