C++ Standard Library C++ STL Library

C++ <cmath> - signbit() Function



The C++ <cmath> signbit() function returns true if the given argument is negative, else returns false. This can be also applied to infinity, NaN and zero (if zero is unsigned, it is considered positive).

Syntax

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

Parameters

x Specify a floating-point value.

Return Value

Returns true (non-zero value) if the argument is negative, else returns false (zero value).

Example:

The example below shows the usage of signbit() function.

#include <iostream>
#include <cmath>

using namespace std;
 
int main (){

  cout<<boolalpha;
  cout<<"signbit(10.5): "<<signbit(10.5)<<"\n";
  cout<<"signbit(1.0/0.0): "<<signbit(1.0/0.0)<<"\n";
  cout<<"signbit(-1.0/0.0): "<<signbit(-1.0/0.0)<<"\n";
  cout<<"signbit(sqrt(-1.0)): "<<signbit(sqrt(-1.0))<<"\n";
  return 0;
}

The output of the above code will be:

signbit(10.5): false
signbit(1.0/0.0): false
signbit(-1.0/0.0): true
signbit(sqrt(-1.0)): true

❮ C++ <cmath> Library