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
| Parameter | Description | 
|---|---|
| x | Value 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:
- The program defines a double precision number numinitialized to 8.0.
- The logb()function computes the logarithm of the absolute value ofnum.
- Since on most platforms FLT_RADIXis 2, the result is the base-2 logarithm, yielding 3.
- The result is printed using printf().
Output:
logb(8.0) = 3.0Example 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:
- A double precision number numis initialized to 0.5.
- The logb()function computes the logarithm of the absolute value ofnum.
- With FLT_RADIXas 2, the logarithm of 0.5 is -1.
- The result is displayed using printf().
Output:
logb(0.5) = -1.0Example 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:
- A double precision number numis initialized to -16.0.
- The logb()function computes the logarithm of the absolute value ofnum, ignoring the sign.
- Since | -16.0 | equals 16.0, the base-2 logarithm is 4.
- The result is output using printf().
Output:
logb(-16.0) = 4.0Example 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:
- A float variable numis set to 32.0f.
- The logbf()function computes the logarithm of the absolute value ofnumusing the floating-point radix.
- With FLT_RADIXequal to 2, the logarithm is 5.
- The result is printed with printf().
Output:
logbf(32.0) = 5.0