log2() Function

The log2() function computes the binary logarithm (base-2 logarithm) of a given value. It is used to determine the power to which the number 2 must be raised to obtain the given value. This function returns the logarithm for positive inputs and handles errors for non-positive inputs according to the library’s error handling settings.


Syntax of log2()

</>
Copy
double log2(double x);
float  log2f(float x);
long double log2l(long double x);

Parameters

ParameterDescription
xThe value for which the binary logarithm is to be computed. Note that if the value is negative, a domain error occurs, and if the value is zero, a pole error may occur depending on the implementation.

When an error occurs due to a negative input, the library either sets errno to EDOM or raises the FE_INVALID floating-point exception. Similarly, a zero input may lead to a pole error with errno set to ERANGE or FE_DIVBYZERO raised.


Examples for log2()

Example 1: Basic Computation of Binary Logarithm

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double value = 16.0;
    double result = log2(value);
    
    printf("Binary logarithm of %.2f is %.2f\n", value, result);
    return 0;
}

Explanation:

  1. A double variable value is initialized to 16.0.
  2. The log2() function calculates the binary logarithm of 16.0.
  3. The result is stored in the variable result and printed.

Program Output:

Binary logarithm of 16.00 is 4.00

Example 2: Computing Logarithm for a Power of Two

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    float value = 8.0f;
    float result = log2f(value);
    
    printf("Binary logarithm of %.2f is %.2f\n", value, result);
    return 0;
}

Explanation:

  1. A float variable value is set to 8.0.
  2. The log2f() function computes the binary logarithm of 8.0.
  3. The result, which should be 3.00, is printed.

Program Output:

Binary logarithm of 8.00 is 3.00

Example 3: Computing Logarithm for a Non-Power-of-Two Value

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double value = 3.0;
    double result = log2(value);
    
    printf("Binary logarithm of %.2f is %.2f\n", value, result);
    return 0;
}

Explanation:

  1. A double variable value is initialized to 3.0.
  2. The log2() function calculates the binary logarithm of 3.0.
  3. The result is stored and printed, demonstrating the function’s handling of non-power-of-two values.

Program Output:

Binary logarithm of 3.00 is 1.58

Example 4: Handling Domain Errors with Negative Input

Program

</>
Copy
#include <stdio.h>
#include <math.h>
#include <errno.h>

int main() {
    double value = -4.0;
    errno = 0;  // Clear errno before calling log2
    double result = log2(value);
    
    if (errno == EDOM) {
        printf("Error: Domain error occurred for input %.2f\n", value);
    } else {
        printf("Binary logarithm of %.2f is %.2f\n", value, result);
    }
    return 0;
}

Explanation:

  1. A double variable value is initialized to -4.0, which is an invalid input for logarithm computation.
  2. The errno variable is cleared before calling log2() to detect any errors.
  3. The function call to log2() results in a domain error, and errno is set to EDOM.
  4. An error message is printed to inform the user about the domain error.

Program Output:

Error: Domain error occurred for input -4.00