C Math ilogb() Function
Example
Return the exponent (the power of 2) of different numbers:
printf("%d\n", ilogb(8.0));
printf("%d\n", ilogb(10.0));
printf("%d", ilogb(0.25));
Definition and Usage
The ilogb() function returns the exponent of x. This is the base 2 logarithm of the absolute value of x, rounded down to a whole number. The result is an int.
For example, 10 is between 8 (23) and 16 (24), so ilogb(10.0) returns 3. The base is really FLT_RADIX, the base that the computer uses to store decimal numbers, but it is 2 on almost all computers.
The ilogb() function is defined in the <math.h> header file.
Tip: logb() does the same, but returns the result as a double. To get the exact base 2 logarithm, use log2().
Syntax
int ilogb(double x);
There are also versions for float and long double values: ilogbf() and ilogbl().
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. A number. |
Technical Details
| Returns: | An int value with the exponent of x. |
|---|
Related Pages
Read more about math in our C Math Tutorial.