function
<cmath> <ctgmath>

log2

 double log2 (double x); float log2f (float x);long double log2l (long double x);
 double log2 (double x); float log2 (float x);long double log2 (long double x); double log2 (T x); // additional overloads for integral types
Compute binary logarithm
Returns the binary (base-2) logarithm of x.

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type).

Parameters

x
Value whose logarithm is calculated.
If the argument is negative, a domain error occurs.

Return Value

The binary logarithm of x: log2x.
If x is negative, it causes a domain error:
If x is zero, it may cause a pole error (depending on the library implementation).

If a domain error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to EDOM.
- And math_errhandling has MATH_ERREXCEPT set: FE_INVALID is raised.

If a pole error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to ERANGE.
- And math_errhandling has MATH_ERREXCEPT set: FE_DIVBYZERO is raised.

Example

1
2
3
4
5
6
7
8
9
10
11
12
/* log2 example */ #include <stdio.h> /* printf */ #include <math.h> /* log2 */ int main () { double param, result; param = 1024.0; result = log2 (param); printf ("log2 (%f) = %f.\n", param, result ); return 0; }

Output:
 log2 (1024.000000) = 10.000000 


See also