Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

signbit — checking a floating-point value's sign

The signbit macro from <math.h> checks whether a floating-point value has a negative sign, including zeros, infinities, and NaNs. A nonzero result need not equal 1.

Version: C23; repository compiler flag, production runtime not attested

Syntax

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

signbit is a macro from <math.h>, not an ordinary function. In the synopsis, real-floating means a real floating type, such as float, double, or long double; it is not a C type name.

The int result is nonzero if and only if the sign is determined to be negative; otherwise it is zero. Use !!signbit(x) to obtain exactly 0 or 1, rather than comparing the result with 1.

This differs from x < 0.0: a supported negative zero has a negative sign but is not less than zero. The macro also covers infinities and NaNs. An unsigned zero is treated as positive; for other unsigned values, the result is implementation-defined. Here, “unsigned” concerns the floating-point value, not the type unsigned int.

Examples

#include <math.h>
#include <stdio.h>

int main(void)
{
    printf("%d %d %d\n",
           !!signbit(-2.0),
           !!signbit(0.0),
           !!signbit(2.0));
    return 0;
}

Result

1 0 0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources