C++ std::ldexp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::ldexp(x, e) calculates x times 2 to the power e. The name means "load exponent". ldexp(1.0, 10) is 1024, and a negative exponent divides: ldexp(3.0, -2) is 3 / 4 = 0.75.

The exponent e is an int. Multiplying by a power of two is exact, because it only changes the exponent of the number and leaves the digits alone. It is the opposite of std::frexp().

std::ldexp() is declared in <cmath> and is part of the group Remainder, scale, and compare. These functions split a number, scale it by a power of two, find its remainder, or pick between two numbers. They work directly on the binary form of a double.

Syntax

Include <cmath> and call std::ldexp. Start with these lines.

C++
double a = std::ldexp(1.0, 10);    // 1024
double b = std::ldexp(0.5, 3);     // 4
double c = std::ldexp(3.0, -2);    // 0.75
CallWhat it does
std::ldexp(1.0, 10)1 * 210. Returns 1024.
std::ldexp(3.0, -2)A negative exponent divides: 3 / 4. Returns 0.75.
std::ldexp(1.5f, 4)The float overload. Returns 24.0f.

The exponent must be an int, not a double. For a decimal exponent, use std::exp2() or std::pow().

Library declarations
C++
float       ldexp(float x, int exp);
double      ldexp(double x, int exp);
long double ldexp(long double x, int exp);
double      ldexp(Integer x, int exp);   // for integer types

Parameters

ParameterDescription
xRequired. The number to scale. It can be float, double, long double, or an integer type.
expRequired. The power of two, as an int. It can be negative.

Return Value

Return ValueDescription
x * 2expThe scaled number, in the type of x.
infThe result is too big for the type (an overflow).
0The result is too small to store (an underflow), or x is 0.

C++ Examples

Example 1 — Scale Several Numbers by Powers of Two

Print ldexp() for different numbers and exponents, including a negative one.

C++
#include <cmath>
#include <iostream>

int main() {
    double xs[] = {1.0, 0.5, 3.0, 1.5, 5.0};
    int es[]    = {10, 3, -2, 4, 0};

    for (int i = 0; i < 5; ++i) {
        std::cout << "ldexp(" << xs[i] << ", " << es[i] << ") = "
                  << std::ldexp(xs[i], es[i]) << '\n';
    }
    return 0;
}

How It Works

  1. Two arrays hold the numbers and their exponents. The loop pairs them by index.
  2. 1 * 210 is 1024, and 0.5 * 23 is 4. 1.5 * 24 is 24.
  3. The negative exponent -2 divides by 4, giving 0.75. An exponent of 0 multiplies by 1, so 5 stays 5.

Example 2 — Doubling and Halving Many Times

Multiplying by 2n is the same as doubling n times. A negative exponent halves.

C++
#include <cmath>
#include <iostream>

int main() {
    double seven = 7.0;
    double hundred = 100.0;

    std::cout << "7 doubled 3 times:    " << std::ldexp(seven, 3) << '\n';
    std::cout << "100 halved 4 times:   " << std::ldexp(hundred, -4) << '\n';
    std::cout << "Same as 7 * 8:        " << (std::ldexp(seven, 3) == seven * 8) << '\n';
    return 0;
}

How It Works

  1. Doubling 7 three times gives 14, 28, then 56. ldexp(7, 3) does this in one step.
  2. A negative exponent halves the number. 100 halved four times is 100 / 16, which is 6.25.
  3. The check against 7 * 8 prints 1. Scaling by a power of two is exact, with no rounding error at all.

Example 3 — Overflow and Underflow

See what happens when the exponent pushes the number past the range of a double.

C++
#include <cmath>
#include <iostream>

int main() {
    std::cout << "ldexp(1, 1023) = " << std::ldexp(1.0, 1023) << '\n';
    std::cout << "ldexp(1, 1024) = " << std::ldexp(1.0, 1024) << '\n';
    std::cout << "ldexp(1, -1075) = " << std::ldexp(1.0, -1075) << '\n';
    return 0;
}

How It Works

  1. 21023 is the biggest power of two that a double can hold. It prints as 8.98847e+307.
  2. One more step, 21024, is too big and becomes inf. The program does not crash.
  3. 2-1075 is smaller than the smallest tiny number a double can store, so it is rounded to 0.

Frequently Asked Questions

std::ldexp(x, e) returns x multiplied by 2 to the power e. For example, ldexp(1.0, 10) is 1024 and ldexp(3.0, -2) is 0.75.
The value x * 2^e as float, double, or long double. If the result is too big it is inf, and if it is too small it is 0 or a tiny number.
ldexp(x, e) multiplies x by 2^e and is exact, because it only changes the exponent. It takes an int exponent. pow(2.0, e) is a general power function that works with any decimal exponent.
It is std::frexp(). frexp(x, &e) splits x into a fraction and a power of two, and ldexp(m, e) puts them back together.
Include <cmath> and write std::ldexp(). That always works and picks the C++ overloads. The global name ::ldexp often works too, but the standard does not promise it, and it may only take a double.

Next: lgamma()

Keep going through the <cmath> functions. The natural logarithm of the gamma function, for big factorials.

lgamma() tutorial →

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.

10 people found this page helpful