Rate This Document
Findability
Accuracy
Completeness
Readability

log2

Compute the base-2 logarithm of the input.

Interface Definition

C interface:

float log2f(float x);

double log2(double x);

long double log2l(long double x);

Fortran interface:

RES = LOG2F(X);

RES = LOG2(X);

Parameters

Parameter

Type

Description

Input/Output

x

  • For log2f, x is of single-precision floating-point type.
  • For log2, x is of double-precision floating-point type.
  • For log2l, x is of long double-precision floating-point type.

Floating-point value of the input data.

Input

Return Value

  • The base-2 logarithm y of x is returned. y ∈ (-INF, +INF)
  • If input x is ±0, the return value is -∞.
  • If input x is 1, the return value is 0.
  • If input x is negative, the return value is NaN.
  • If input x is -∞, the return value is NaN.
  • If input x is +∞, the return value is +∞.
  • If input x is NaN, the return value is NaN.

Dependency

C: "km.h"

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 
    !