Name

logb

Synopsis

Obtains the exponent of a floating-point number

#include <math.h>
doublelogb( double x );
float logbf( float x );
long double logbl( long double x );

The logb() functions return the exponent of their floating-point argument. If the argument is not normalized, logb() returns the exponent of its normalized value. If the argument is zero, logb() may incur a domain error, depending on the implementation. (In our example below, using the GNU C library, no domain error occurs.)

Example

double x[ ] = { 0, 0, 0.7, 1.8, 1234, INFINITY };

x[1] = nexttoward( 0.0, 1.0 );

for ( int i = 0; i < sizeof( x ) / sizeof( double ); i++ )
{
  printf( "The exponent in the binary representation of %g is %g.\n",
          x[i],logb( x[i] ) );
  if ( errno == EDOM || errno == ERANGE )
    perror( _  _FILE_  _ );
}

This code produces the following output:

The exponent in the binary representation of 0 is -inf.
The exponent in the binary representation of 4.94066e-324 is -1074.
The exponent in the binary representation of 0.7 is -1.
The exponent in the binary representation of 1.8 is 0.
The exponent in the binary representation of 1234 is 10.
The exponent in the binary representation of inf is inf.

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.