C logb() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, logb() returns the exponent of a floating-point number as a double. Think of x as significand × radixexponent. For binary floating point, logb(16) is 4 because 16 is 1 × 24 in that form.

You pass one double. The result is a double holding the unbiased exponent. Include <math.h>. On GCC, link with -lm. For an int exponent, see ilogb().

logb looks at the floating-point encoding scale, not a user-chosen log base in the everyday sense. It pairs well with scalbn() and frexp().

Syntax

Pass the floating-point number whose exponent you want.

C
logb(16.0);
CallWhat it does
logb(16.0)Returns 4.0 for a typical binary floating-point radix.

Include <math.h>. On GCC, link with -lm. Special values like zero, infinity, and NaN have special results.

Library declarations
C
double logb(double x);
float logbf(float x);
long double logbl(long double x);

Parameters

ParameterDescription
xRequired. The floating-point value whose exponent you want to read.

Return Value

Return ValueDescription
doubleThe unbiased exponent of x. For zero it may be -infinity; for NaN it stays NaN.

C Examples

Example 1 — Basic Exponent Read

Read exponents for a few powers of two.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.1f\n", logb(1.0));
    printf("%.1f\n", logb(8.0));
    printf("%.1f\n", logb(0.25));
    return 0;
}

How It Works

  1. logb(1). 1 is 1 × 2^0, so the exponent is 0.
  2. logb(8). 8 is 1 × 2^3, so the exponent is 3.
  3. logb(0.25). 0.25 is 1 × 2^-2, so the exponent is -2.
  4. Sign ignored. The magnitude sets the exponent.

Example 2 — Compare Magnitudes

Use exponents to see which value is larger in scale.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double a = 1.5e3;
    double b = 9.0e1;

    printf("exp a = %.0f\n", logb(a));
    printf("exp b = %.0f\n", logb(b));
    return 0;
}

How It Works

  1. Different sizes. a is much larger than b.
  2. Read exponents. Larger magnitude usually means a larger exponent.
  3. Quick compare. You can sort by scale without full division.
  4. Still a double. Remember the return type is floating, not int.

Example 3 — Rebuild with scalbn

Peel off the exponent, then put a number back on that scale.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 48.0;
    double e = logb(x);
    double y = scalbn(1.5, (int)e);

    printf("logb = %.0f\n", e);
    printf("y    = %.1f\n", y);
    return 0;
}

How It Works

  1. Read exponent. logb(48) is 5 on a binary system.
  2. Pick significand. Here we choose 1.5 on purpose.
  3. scalbn rebuilds. 1.5 × 2^5 equals 48.
  4. Related tools. frexp and ldexp solve a similar split/join job.

Frequently Asked Questions

logb(x) returns the floating-point exponent of x as a double.
x is required. It is the value whose exponent you want.
A double holding the unbiased exponent of x.
Include <math.h>. On GCC, link with -lm.
log2() is the mathematical base-2 log. logb() returns the floating-point encoding exponent.

Next: lrint()

Round a floating-point value to long using the current rounding mode.

lrint() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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