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