Previous Page
Next Page

signbit

Ascertains whether a floating-point number is negative

#include <math.h>
int signbit( x );

The argument of the signbit( ) macro can have any real floating-point typefloat, double, or long doubleand can have any numeric or other value, including INFINITY, NaN, or 0. The macro ascertains whether the argument's value is negative (whether its sign bit is set, to put it more precisely), and returns a nonzero value, or true, if it is. Otherwise, signbit( ) returns 0.

Example

double x[ ] = { -0.0,  187.234,  sqrt( -1.0 ),  1.0 / -0.0 };

for ( int i = 0 ; i < ( sizeof(x) / sizeof(double)) ; i++ )
  printf( "x[%d] equals %lF, and is%s negative.\n",
          i, x[i], signbit( x[i] ) ? "" : " not" );

The behavior of this example depends on whether the compiler supports negative zero values in floating-point constants, and whether the undefined arithmetic operations in the array initialization cause fatal exceptions. Compiled with GCC 3.3.5 and the GNU C library, this code produces the following output:

x[0] equals -0.000000, and is negative.
x[1] equals 187.234000, and is not negative.
x[2] equals NAN, and is negative.
x[3] equals -INF, and is negative.

See also the example for isunordered( ) in this chapter.

See Also

fpclassify( ), isfinite( ), isinf( ), isnan( ), isnormal( )


Previous Page
Next Page