C++ Math signbit
The function determines if a provided number is negative by checking its sign. It will output 1 if the number is negative, otherwise it will output 0.
The signbit function is applicable to infinite, NAN, and zero values as well.
Syntax
Suppose a number is 'x'.Syntax would be:
bool signbit(float x);
bool signbit(double x);
bool signbit(long double x);
bool signbit(integral x);
Parameter
x : It is a floating point value.
Return value
It returns 1 when the value of x is negative; otherwise, it returns 0, indicating a non-negative value.
Example 1
Let's examine a straightforward instance where the value assigned to x is 9.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=9;
cout<<"value of x is :"<<x<<'\n';
cout<<"signbit(x) : "<<signbit(x);
return 0;
}
Output:
value of x is :9
signbit(x) : 0
In this instance, the signbit(x) function identifies the positivity of the value of x, resulting in a return value of 0.
Example 2
Let's examine a straightforward scenario where the value assigned to x is -2.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x= -2;
cout<<"value of x is : "<<x<<'\n';
cout<<"signbit(x) : "<<signbit(x);
return 0;
}
Output:
value of x is : -2
signbit(x) : 1
In this instance, the signbit(x) function identifies whether the value of x is negative, and subsequently, it outputs 1.
Example 3
Let's examine a straightforward scenario where the value of x is unbounded.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=11.0/0.0;
cout<<"value of x is : "<<x<<'\n';
cout<<"signbit(x) : "<<signbit(x);
return 0;
}
Output:
value of x is : inf
signbit(x) : 0
In this instance, the signbit(x) function identifies that the value of x represents positive infinity, resulting in a return value of 0.