C++ cmath ilogb() Function
Example
Return the exponent (the power of 2) of different numbers:
cout << ilogb(8.0) << "\n";
cout << ilogb(10.0) << "\n";
cout << 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 <cmath> header file.
Tip: logb() does the same, but returns the result as a double. To get the exact base 2 logarithm, use log2().
Syntax
ilogb(double x)
The function also works with float and long double values.
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 Math Tutorial.