Name
signbit
Synopsis
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 type—float
,
double
, or long double
—and 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
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.