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
Call
What 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
Parameter
Description
x
Required. The number to scale. It can be float, double, long double, or an integer type.
n
Required. The power, as a long. It can be negative.
Return Value
Return Value
Description
x * radixn
The scaled number, in the type of x.
inf
The result is too big for the type (an overflow).
0
The 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.
22000 is far beyond the largest double, so the result is inf.
2-2000 is below the smallest double, so it becomes 0. A huge exponent like 1000000 also gives inf at once.
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;
}
Output
n = -3: 0.75 and 0.75, equal: 1
n = 0: 6 and 6, equal: 1
n = 4: 96 and 96, equal: 1
How It Works
The loop calls scalbn() with an int and scalbln() with the same number as a long.
For n = -3 both give 6 / 8 = 0.75. For n = 0 both give 6. For n = 4 both give 96.
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.