log2
Compute the base-2 logarithm of a real number.
Interface Definition
C interface:
float log2f(float x);
double log2(double x);
Fortran interface:
RES = LOG2F(X);
RES = LOG2(X);
Parameters
Parameter |
Type |
Description |
Input/Output |
|---|---|---|---|
x |
|
Floating-point value of the input data |
Input |
Return Value
- The base-2 logarithm y of x is returned. y ∈ (-INF, +INF)
- If the input x is ±0, the return value is -∞.
- If the input x is 1, the return value is 0.
- If the input x is a negative number, the return value is NaN.
- If the input x is +∞, the return value is +∞.
- If the input x is NaN, the return value is NaN.
Dependencies
C: "km.h"
Fortran: "km.f03"
Examples
C interface:
// typical usage
double x1 = 2.0, x2 = 0.5, x3 = 125, x4 = 5;
// special handing
double a = 0.0, b = INFINITY, c = -INFINITY, d = NAN, e = 1.0;
// print result
printf("base-5 logarithm of 125 = %.15f\n", log2(x3)/log2(x4));
printf("log2(1.0) = %.15f\n", log2(e));
printf("log2(2.0) = %.15f\n", log2(x1));
printf("log2(0.5) = %.15f\n", log2(x2));
printf("log2(0.0) = %.15f\n", log2(a));
printf("log2(INFINITY) = %.15f\n", log2(b));
printf("log2(-INFINITY) = %.15f\n", log2(c));
printf("log2(NAN) = %.15f\n", log2(d));
/*
* base-5 logarithm of 125 = 3.000000000000000
* log2(1.0) = 0.000000000000000
* log2(2.0) = 1.000000000000000
* log2(0.5) = -1.000000000000000
* log2(0.0) = -inf
* log2(INFINITY) = inf
* log2(-INFINITY) = nan
* log2(NAN) = nan
*
* */
Fortran interface:
REAL(8) :: X = 0.5
PRINT*, LOG2(X)
!
! OUTPUT
! -1.000000000000000
!
Parent topic: KML_MATH Library Functions