In C, ilogb() returns the unbiased binary exponent of a double as an int. For a normal number, if x is written as significand × 2e, then ilogb(x) is that exponent e. ilogb(16.0) is 4 because 16 is 1 × 24 in that form.
You pass one floating-point value. The return type is int, not double. Include <math.h>. Related helpers: frexp() also exposes an exponent, and logb() returns a floating exponent.
Syntax
Pass the floating-point number. Start with this call.
C
ilogb(16.0);
Call
What it does
ilogb(16.0)
Returns 4, the power of two for 16.
ilogb(0.5)
Returns -1, because 0.5 is 1 × 2^-1.
Include <math.h>. On GCC, link with -lm. Special inputs may return FP_ILOGB0, FP_ILOGBNAN, or INT_MAX for infinity — check your library docs for edge cases.
Library declarations
C
int ilogb(double x);
int ilogbf(float x);
int ilogbl(long double x);
Parameters
Parameter
Description
x
Required. The floating-point value whose exponent you want.
Return Value
Return Value
Description
int exponent
Unbiased exponent of x for finite non-zero values. Special macros/values apply for zero, NaN, and infinity.
16.0. Stored as significand × 24, so the result is 4.
0.5. Half is 2-1, so the result is -1.
3.0. Between 2 and 4, so the unbiased exponent is 1.
Example 2 — Rebuild with scalbn
Peel off the exponent, then put a value back on that scale.
C
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 12.0;
int e = ilogb(x);
double y = scalbn(1.0, e);
printf("e = %d\n", e);
printf("y = %.1f\n", y);
return 0;
}
Output
e = 3
y = 8.0
How It Works
Read exponent.ilogb(12) is 3 because 12 sits in [8, 16).
Build 2^e.scalbn(1.0, 3) is 8.
Meaning. You recovered the power-of-two bin for x.
Related.frexp returns the fraction and writes the exponent through a pointer.
Example 3 — Compare Scale Without Logs
A higher ilogb means a larger magnitude class.
C
#include <stdio.h>
#include <math.h>
int main(void) {
double a = 100.0;
double b = 5.0;
if (ilogb(a) > ilogb(b)) {
printf("a is on a bigger scale\n");
} else {
printf("b is bigger or equal scale\n");
}
return 0;
}
Output
a is on a bigger scale
How It Works
Two values. 100 and 5.
Compare exponents.ilogb(100) is greater than ilogb(5).
Interpretation. 100 lives in a higher power-of-two bin.
Careful. This is about scale, not a full numeric compare.
Frequently Asked Questions
ilogb() returns the unbiased binary exponent of a floating-point value as an int.
x is required. It is the floating-point value to examine.
An int exponent for finite non-zero x. Special values apply for zero, NaN, and infinity.
Include <math.h>. On GCC, link with -lm.
frexp() returns a fraction and writes the exponent through a pointer. ilogb() returns only the exponent as an int.