Rate This Document
Findability
Accuracy
Completeness
Readability

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

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

Floating-point value of the input data

Input

Return Value

  1. The base-2 logarithm y of x is returned. y ∈ (-INF, +INF)
  2. If the input x is ±0, the return value is -∞.
  3. If the input x is 1, the return value is 0.
  4. If the input x is a negative number, the return value is NaN.
  5. If the input x is +∞, the return value is +∞.
  6. 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 
    !