C++ std::scalbn() Function

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

Definition

In C++, std::scalbn(x, n) scales a number by a power of the machine radix. The radix is called FLT_RADIX and is 2 on all common computers. So scalbn(1.5, 3) is 1.5 * 23 = 12.

It is the general form of std::ldexp(). When the radix is 2, the two give the same results. The multiplication is exact, because only the exponent changes. The second argument is an int. For a long exponent, see std::scalbln().

std::scalbn() 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::scalbn. Start with these lines.

C++
double a = std::scalbn(1.5, 3);     // 12
double b = std::scalbn(10.0, -1);    // 5
float  c = std::scalbn(3.0f, 2);     // 12.0f
CallWhat it does
std::scalbn(1.5, 3)1.5 * 23. Returns 12.
std::scalbn(10.0, -1)A negative exponent divides by the radix. Returns 5.
std::scalbn(3.0f, 2)The float overload. Returns 12.0f.

On every common computer FLT_RADIX is 2. You can check it with #include <cfloat>.

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

Parameters

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

Return Value

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

C++ Examples

Example 1 — Scale Several Numbers

Print scalbn() for different numbers and powers, including a negative power and a negative number.

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

int main() {
    double xs[] = {1.5, 10.0, 3.0, 0.1, -2.0};
    int ns[]    = {3, -1, 2, 0, 5};

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

How It Works

  1. Two arrays hold the numbers and the powers, and the loop uses the same index for both.
  2. 1.5 * 23 is 12, and 3 * 22 is also 12. 10 * 2-1 is 5.
  3. A power of 0 leaves 0.1 unchanged. The negative number -2 times 25 is -64, and the sign is kept.

Example 2 — scalbn() Matches ldexp()

Check the radix, and compare scalbn() with ldexp() for the same inputs.

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

int main() {
    double a = std::scalbn(5.0, 4);
    double b = std::ldexp(5.0, 4);

    std::cout << "FLT_RADIX = " << FLT_RADIX << '\n';
    std::cout << "scalbn(5, 4) = " << a << '\n';
    std::cout << "ldexp(5, 4)  = " << b << '\n';
    std::cout << "Equal: " << (a == b) << '\n';
    return 0;
}

How It Works

  1. FLT_RADIX comes from <cfloat>. On common computers it is 2.
  2. scalbn(5, 4) is 5 * 24, which is 80. ldexp(5, 4) does the same job and gives 80.
  3. The two values are equal, so the test prints 1. On a computer with another radix the results could differ, but that is very rare.

Example 3 — Scale a Number into the Range 1 to 2

Use ilogb() to find the exponent, then scalbn() with the negative exponent to scale the number down to a value between 1 and 2.

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

int main() {
    for (double x : {40.0, 0.3, 1000.0}) {
        int e = std::ilogb(x);
        double m = std::scalbn(x, -e);
        std::cout << x << " = " << m << " * 2^" << e << '\n';
    }
    return 0;
}

How It Works

  1. std::ilogb(x) returns the power of two just below x. For 40 that is 5, because 25 = 32.
  2. scalbn(x, -e) divides by that power of two. 40 / 32 is 1.25, and 0.3 * 4 is 1.2.
  3. 1000 has the exponent 9, and 1000 / 512 is 1.95312. Every result is at least 1 and less than 2. This is different from frexp(), which gives 0.5 to 1.

Frequently Asked Questions

std::scalbn(x, n) returns x multiplied by FLT_RADIX to the power n. On normal computers FLT_RADIX is 2, so it works exactly like ldexp().
The value x * FLT_RADIX^n 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.
On computers with a radix of 2, which is almost all of them, there is no difference. scalbn() uses FLT_RADIX, and ldexp() always uses 2. Both are exact.
Only the type of the exponent. scalbn() takes an int, and scalbln() takes a long, which allows a larger range of exponents.
Include <cmath> and write std::scalbn(). That always works and picks the C++ overloads. The global name ::scalbn often works too, but the standard does not promise it, and it may only take a double.

Next: signbit()

Keep going through the <cmath> functions. Check if a number has a negative sign.

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

14 people found this page helpful