In C++, std::logb(x) tells you the binary exponent of a number. Every double is stored as a value from 1 up to 2, multiplied by a power of two. logb() gives that power.
For 10, which is 1.25 * 23, the answer is 3. In other words, logb(x) is the whole-number part of log2(|x|), rounded down. The result is returned as a double. If you want an int, use std::ilogb().
std::logb() is declared in <cmath> and is part of the group Exponential and logarithm. Exponentials and logarithms undo each other. A logarithm of zero is -inf, and a logarithm of a negative number is NaN.
Syntax
Include <cmath> and call std::logb. Start with these lines.
C++
double a = std::logb(8.0); // double version
float b = std::logb(8.0f); // float version
Call
What it does
std::logb(8.0)
Passes a double and returns a double. Here the result is 3.
std::logb(8.0f)
Passes a float, so the float overload runs and returns a float.
std::logb(40)
An integer is converted to double. Returns 5 as a double.
Write std::logb so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float logb(float x);
double logb(double x);
long double logb(long double x);
double logb(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. A floating-point number. The sign is ignored. An integer is converted to double.
Return Value
Return Value
Description
Exponent
The binary exponent of x, as a floating-point value such as 3.0.
-inf
Returned when x is 0.
inf / NaN
logb(inf) is inf, and logb(NaN) is NaN.
C++ Examples
Example 1 — Binary Exponents of Several Numbers
Print the binary exponent of a few numbers. A number between two powers of two gets the lower power.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {8.0, 10.0, 0.1, 1.0, 1000.0};
for (double v : values) {
std::cout << "logb(" << v << ") = " << std::logb(v) << '\n';
}
return 0;
}
-16.0 has the same size as 16.0, and 16 is 24. The exponent is 4.
Both lines print 4, which shows that logb() ignores the minus sign.
There is no power of two that gives zero, so logb(0) returns -inf.
Example 3 — Round Down to a Power of Two
Combine logb() with exp2() to find the biggest power of two that is not larger than a number.
C++
#include <cmath>
#include <iostream>
int main() {
int sizes[] = {5, 100, 1000};
for (int n : sizes) {
double power = std::exp2(std::logb(n));
std::cout << n << " -> " << power << '\n';
}
return 0;
}
Output
5 -> 4
100 -> 64
1000 -> 512
How It Works
logb(100) is 6, because 100 is between 64 and 128.
exp2(6) turns the exponent back into the power of two, 64. For 5 the exponent is 2, giving 4.
1000 has the exponent 9, so the bucket is 512. This is how memory allocators group sizes.
Frequently Asked Questions
std::logb(x) returns the binary exponent of x as a double. It is the same as floor(log2(abs(x))), so logb(10.0) is 3.
The exponent of x (a power of two) as a floating-point value. logb(0) is -inf, and logb(inf) is inf.
They give the same exponent. logb() returns it as a double, and ilogb() returns it as an int. For logb(0) you get -inf, while ilogb(0) returns the special int value FP_ILOGB0.
Not quite. log2(10) is 3.32193, but logb(10) is 3. logb() gives only the whole-number exponent, which is floor(log2(abs(x))) for normal numbers.
Include <cmath> and write std::logb(). That always works and picks the C++ overloads. The global name ::logb often works too, but the standard does not promise it, and it may only take a double.