In this C++ tutorial, you will learn how to return a quiet NaN value of type double. using nan() function of cmath, with syntax and examples.

C++ nan()

C++ nan() returns a quiet NaN value of type double.

NaN stands for Not-a-Number.

Syntax

The syntax of C++ nan() is

</>
Copy
nan(x)

where

ParameterDescription
xA string, specifically of type const char*. An empty string would suffice.

Returns

nan.

The synopsis of nan() function is

</>
Copy
double nan(const char* arg);

Example

In this example, we just pass an empty string to nan() function and print the value returned.

C++ Program

</>
Copy
#include <iostream>
#include<cmath>
using namespace std;

int main() {
    double result = nan("");
    cout << "nan() : " << result << endl;
}

Output

nan() : nan
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned the syntax of C++ nan(), and how to use this function to get a quiet NaN value, with the help of examples.