What is logb in C?

The logb function is defined in the <math.h> header file in C. It takes in a single parameter x of type double. It then computes the logarithm of x|x| using FLT_RADIX as the base and returns the result of the double type.

FLT_RADIX has a value of 2 in most platforms. Thus, the function performs similar to log2 for positive values.

The illustration below shows how the logb function works:

How does logb work?

Declaration

The logb function is defined as follows:

double logb(double x);

Parameters

The logb function takes a single value of type double as a parameter.

Return value

The logb function returns the computation of type double.

Error handling

The logb function returns special values for certain arguments:

x Return Value
0 -INFINITY
INFINITY +INFINITY
NaN NaN

Example

The following code snippet shows how we can use the logb function:

#include <stdio.h> // include header for printf
#include <math.h> // include header for logb
int main ()
{
double x, result;
x = 8.0;
result = logb(x);
printf("logb (%f) = %f.\n", x, result);
return 0;
}

The following code snippet shows how error handling in the logb function works:

#include <stdio.h> // include header for printf
#include <math.h> // include header for logb
int main ()
{
double x, y, result;
x = 0.0;
result = logb(x);
printf("logb (%f) = %f.\n", x, result);
y = INFINITY;
result = logb(y);
printf("logb (%f) = %f.\n", y, result);
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved