C++ isnan()
Anonymous contributor
Published Nov 15, 2022
Contribute to Docs
The isnan() function returns a number telling whether a given value is a NaN value.
Syntax
isnan(x)
The x parameter is a floating-point value that can be of type float, double, long double, or int. The return value is 1 if x is NaN. Otherwise, 0 is returned.
Note: Common cases of a
NaNvalue include:
- Taking the square root of a negative value
- Dividing positive or negative zero by positive or negative zero (0/0)
Example
In the example below, the isnan() function checks whether or not the value of x is NaN:
#include <iostream>#include<math.h>using namespace std;int main() {float x = 0.0 / 0.0;cout << "value of isnan(x) is: " << isnan(x) << "\n";return 0;}
This produces the following output:
value of isnan(x) is: 1
Codebyte Example
In the example below, each possible data type is applied to the isnan() function. The application of the int type involves the sqrt() function:
All contributors
- Anonymous contributor
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.
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