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
Call
What 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
Parameter
Description
x
Required. The number to scale. It can be float, double, long double, or an integer type.
n
Required. The power, as an int. It can be negative.
Return Value
Return Value
Description
x * radixn
The scaled number, in the type of x. The radix is 2 on common computers.
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 scalbn() for different numbers and powers, including a negative power and a negative number.
FLT_RADIX comes from <cfloat>. On common computers it is 2.
scalbn(5, 4) is 5 * 24, which is 80. ldexp(5, 4) does the same job and gives 80.
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;
}
std::ilogb(x) returns the power of two just below x. For 40 that is 5, because 25 = 32.
scalbn(x, -e) divides by that power of two. 40 / 32 is 1.25, and 0.3 * 4 is 1.2.
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.