C Math logb() Function
Example
Return the exponent (the power of 2) of different numbers:
printf("%f\n", logb(8.0));
printf("%f\n", logb(-10.0));
printf("%f", logb(0.25));
Definition and Usage
The logb() 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 a double.
For example, 10 is between 8 (23) and 16 (24), so logb(-10.0) returns 3. The sign of x does not matter. 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 logb() function is defined in the <math.h> header file.
Tip: ilogb() does the same, but returns an int. To get the exact base 2 logarithm, use log2().
Syntax
double logb(double x);
There are also versions for float and long double values: logbf() and logbl().
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. A number. |
Technical Details
| Returns: | A double value with the exponent of x. |
|---|
Related Pages
Read more about math in our C Math Tutorial.