signbit()

The signbit() function returns true if the sign of the argument is negative and false if its sign is positive.

Syntax

The cmath library must be added at the top of the file.

std::signbit(n);

Argument n must be of type double/float/long double/int, and the return value will be of that type. This function also detects the signs of zeroes, infinities, and NaNs.

Example

#include <iostream>
#include <cmath>
int main()
{
double a = 9.0, b = 0.0;
// Here, c is infinity because a number divided by 0
// evaluates to positive infinity when there is a limit.
double c = a/b;
// If c is false, print out that it tends to positive infinity.
if (std::signbit(c) == false)
{
std::cout << "c tends to positive infinity";
}
else
{
std::cout << "c tends to negative infinity";
}
return 0;
}

Codebyte Example

The example below outputs a string about whether the integer is positive or negative:

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C++ on Codecademy