December 2005
Beginner to intermediate
618 pages
20h 19m
English
logb
Obtains the exponent of a floating-point number
#include <math.h> doublelogb( doublex); floatlogbf( floatx); long doublelogbl( long doublex);
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.)
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.
Read now
Unlock full access