nan()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Nov 17, 2022
Contribute to Docs

The nan() function returns a quiet NaN (not a number) value of type double.

Syntax

nan(x)

The x argument is a character string that can be used by library implementations to distinguish different NaN values.

Example

The following example uses the nan() function to return a NaN value:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double number1 = 1.0;
double number2 = nan("");
if (isnan(number1)) {
cout << "number1 is not a number";
}
if (isnan(number2)) {
cout << "number2 is not a number";
}
return 0;
}

This produces the following output:

number2 is not a number

Codebyte Example

The following example is runnable and uses the nan() function to return a NaN value:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn C++ on Codecademy