C++ std::logb() Function

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

Definition

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
CallWhat 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

ParameterDescription
xRequired. A floating-point number. The sign is ignored. An integer is converted to double.

Return Value

Return ValueDescription
ExponentThe binary exponent of x, as a floating-point value such as 3.0.
-infReturned when x is 0.
inf / NaNlogb(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;
}

How It Works

  1. The loop gives each number to std::logb().
  2. 8 is exactly 23, so its exponent is 3. 10 is between 8 and 16, so it also gets 3. 1 is 20.
  3. 0.1 is about 1.6 * 2-4, so the exponent is -4. 1000 is between 512 and 1024, so it gets 9.

Example 2 — Negative Numbers and Zero

The sign is ignored, and zero has no exponent at all.

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

int main() {
    std::cout << "logb(-16) = " << std::logb(-16.0) << '\n';
    std::cout << "logb(16)  = " << std::logb(16.0) << '\n';
    std::cout << "logb(0)   = " << std::logb(0.0) << '\n';
    return 0;
}

How It Works

  1. -16.0 has the same size as 16.0, and 16 is 24. The exponent is 4.
  2. Both lines print 4, which shows that logb() ignores the minus sign.
  3. 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;
}

How It Works

  1. logb(100) is 6, because 100 is between 64 and 128.
  2. exp2(6) turns the exponent back into the power of two, 64. For 5 the exponent is 2, giving 4.
  3. 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.

Next: lrint()

Keep going through the <cmath> functions. Round to the nearest long integer.

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

9 people found this page helpful