C++ signbit()

Christine_Yang's avatar
Published Nov 4, 2022Updated Dec 21, 2022

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

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;
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:

Code
Output

All contributors

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours