C++ cmath signbit function


The C++ cmath signbit function determines the sign of the argument.

bool signbit(float x);
bool signbit(double x);
bool signbit(long double x);

Parameters:
x – A value whose sign is to be determined.

Return type
bool-Returns true only if the sign of the parameter is negative.

The function determines the sign of all the type nan,infinity,0,normal and subnormal.If 0 is unsigned type it’s sign is considered as +ve.

Code example

/*Passing normal type*/
cout<< signbit(9) << endl
<< signbit(-23) << endl ;

/*Passing nan type*/
cout<< signbit(NAN) << endl
<< signbit(-NAN) << endl ;

/*Passing infinity type*/
cout<< signbit(INFINITY) << endl
<< signbit(-INFINITY) << endl ;

/*Passing 0 (zero)*/
short signed ss=0b1000000000000000 ; /*A binary digit of -0 (minus zero), check the C++14 features to know more about binary literals */

cout<< signbit(0) << endl
<< signbit(ss) << endl
<< signbit(-0.0) << endl;

Output

0
1
0
1
0
1
0
1
1
Link: C++14 binary literals