logb() Function

The logb() function computes the logarithm of the absolute value of a floating-point number, using the implementation-defined floating-point radix as its base. On most platforms, this base is 2, making it equivalent to a base-2 logarithm for positive inputs.


Syntax of logb()

</>
Copy
double logb(double x);
float logbf(float x);
long double logbl(long double x);

Parameters

ParameterDescription
xValue whose logarithm is to be computed.

Return Value

Returns the logarithm of the absolute value of the given input, using FLT_RADIX as the base. On most systems where FLT_RADIX equals 2, the result is equivalent to a base-2 logarithm.

Notes

If the input is zero, the function may raise a domain or pole error. Depending on the system’s math error handling, this might set the global variable errno (to EDOM or ERANGE) or raise floating-point exceptions such as FE_INVALID or FE_DIVBYZERO.


Examples for logb()

Example 1: Calculating the Logarithm of a Positive Number

Program

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

int main() {
    double num = 8.0;
    double result = logb(num); // Expected log2(8.0) = 3 on systems with FLT_RADIX = 2
    printf("logb(%.1f) = %.1f\n", num, result);
    return 0;
}

Explanation:

  1. The program defines a double precision number num initialized to 8.0.
  2. The logb() function computes the logarithm of the absolute value of num.
  3. Since on most platforms FLT_RADIX is 2, the result is the base-2 logarithm, yielding 3.
  4. The result is printed using printf().

Output:

logb(8.0) = 3.0

Example 2: Calculating the Logarithm of a Fractional Value

Program

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

int main() {
    double num = 0.5;
    double result = logb(num); // Expected log2(0.5) = -1 on systems with FLT_RADIX = 2
    printf("logb(%.1f) = %.1f\n", num, result);
    return 0;
}

Explanation:

  1. A double precision number num is initialized to 0.5.
  2. The logb() function computes the logarithm of the absolute value of num.
  3. With FLT_RADIX as 2, the logarithm of 0.5 is -1.
  4. The result is displayed using printf().

Output:

logb(0.5) = -1.0

Example 3: Handling a Negative Input

Program

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

int main() {
    double num = -16.0;
    double result = logb(num); // The function computes the logarithm of the absolute value, equivalent to log2(16.0) = 4
    printf("logb(%.1f) = %.1f\n", num, result);
    return 0;
}

Explanation:

  1. A double precision number num is initialized to -16.0.
  2. The logb() function computes the logarithm of the absolute value of num, ignoring the sign.
  3. Since | -16.0 | equals 16.0, the base-2 logarithm is 4.
  4. The result is output using printf().

Output:

logb(-16.0) = 4.0

Example 4: Using the Float Variant logbf()

Program

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

int main() {
    float num = 32.0f;
    float result = logbf(num); // Expected log2(32.0) = 5 on systems with FLT_RADIX = 2
    printf("logbf(%.1f) = %.1f\n", num, result);
    return 0;
}

Explanation:

  1. A float variable num is set to 32.0f.
  2. The logbf() function computes the logarithm of the absolute value of num using the floating-point radix.
  3. With FLT_RADIX equal to 2, the logarithm is 5.
  4. The result is printed with printf().

Output:

logbf(32.0) = 5.0