signbit()
Published Nov 4, 2022Updated Dec 21, 2022
Contribute to Docs
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 NaN
s.
Example
#include <iostream>#include <cmath>int main() {double a = 9.0, b = 0.0;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;}
In the example above, c
is infinity because a number divided by 0 evaluates to positive infinity when there is a limit.
This will output:
c tends to positive infinity
Codebyte Example
The example below outputs a string about whether the integer is positive or negative:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.