C++ std::scalbln() Function

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

Definition

In C++, std::scalbln(x, n) multiplies a number by a power of the machine radix, which is 2 on all common computers. So scalbln(1.5, 3L) is 1.5 * 23 = 12.

The only difference from std::scalbn() is the type of the second argument: a long instead of an int. Use it when the exponent already is a long, so you do not need a cast. Like scalbn(), the multiplication is exact.

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

C++
double a = std::scalbln(1.5, 3L);       // 12
double b = std::scalbln(10.0, -1L);      // 5
double c = std::scalbln(1.0, 2000L);     // inf
CallWhat it does
std::scalbln(1.5, 3L)1.5 * 23. Returns 12.
std::scalbln(10.0, -1L)A negative exponent divides. Returns 5.
std::scalbln(1.0, 2000L)Far too large for a double. Returns inf.

The exponent is a long, so a literal can be written with an L, as in 3L. A plain int also works and is converted.

Library declarations
C++
float       scalbln(float x, long n);
double      scalbln(double x, long n);
long double scalbln(long double x, long n);
double      scalbln(Integer x, long 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 a long. It can be negative.

Return Value

Return ValueDescription
x * radixnThe scaled number, in the type of x.
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 scalbln() for different numbers and long exponents.

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

int main() {
    double xs[] = {1.5, 10.0, 0.25, 7.0};
    long ns[]   = {3, -1, 4, 0};

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

How It Works

  1. The exponents are stored in a long array. They are passed directly, without a cast.
  2. 1.5 * 23 is 12. 0.25 * 24 is 4, because 0.25 is 2-2 and the powers add up.
  3. The negative exponent in scalbln(10, -1) divides by 2 to give 5, and an exponent of 0 leaves 7 unchanged.

Example 2 — Exponents Far Outside the Range

A long exponent can be huge. The function still returns a clean inf or 0.

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

int main() {
    std::cout << "scalbln(1, 2000)    = " << std::scalbln(1.0, 2000L) << '\n';
    std::cout << "scalbln(1, -2000)   = " << std::scalbln(1.0, -2000L) << '\n';
    std::cout << "scalbln(1, 1000000) = " << std::scalbln(1.0, 1000000L) << '\n';
    std::cout << "scalbln(-1, 5000)   = " << std::scalbln(-1.0, 5000L) << '\n';
    return 0;
}

How It Works

  1. 22000 is far beyond the largest double, so the result is inf.
  2. 2-2000 is below the smallest double, so it becomes 0. A huge exponent like 1000000 also gives inf at once.
  3. The sign of x is kept when the result overflows, so -1 scaled by 25000 is -inf.

Example 3 — scalbln() and scalbn() Give the Same Result

For exponents that fit in an int, the two functions agree. Compare them on a few inputs.

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

int main() {
    double x = 6.0;

    for (int n : {-3, 0, 4}) {
        double a = std::scalbn(x, n);
        double b = std::scalbln(x, static_cast<long>(n));
        std::cout << "n = " << n << ": " << a << " and " << b
                  << ", equal: " << (a == b) << '\n';
    }
    return 0;
}

How It Works

  1. The loop calls scalbn() with an int and scalbln() with the same number as a long.
  2. For n = -3 both give 6 / 8 = 0.75. For n = 0 both give 6. For n = 4 both give 96.
  3. The results are equal every time. Use scalbln() only when your exponent is already a long.

Frequently Asked Questions

std::scalbln(x, n) returns x multiplied by FLT_RADIX to the power n, where n is a long. It is the same as scalbn() but accepts a longer exponent type.
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.
When your exponent is a long. For a normal int exponent both give the same answer. The long version is mainly there to follow the C standard.
On computers with radix 2, which is almost all of them, the result is the same. ldexp() takes an int exponent, and scalbln() takes a long.
Include <cmath> and write std::scalbln(). That always works and picks the C++ overloads. The global name ::scalbln often works too, but the standard does not promise it, and it may only take a double.

Next: scalbn()

Keep going through the <cmath> functions. Multiply a number by the radix raised to an int power.

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

13 people found this page helpful