Join our newsletter for the latest updates.

C++ nan()

The nan() function in C++ returns a quiet NaN (Not-A-Number) value of type double.

The function is defined in <cmath> header file.

nan() prototype

double nan (const char* arg);

Similarly, nanf and nanl return NaN values of type float and long double, respectively.

nan() Parameters

An implementation-specific C-string. If the string is empty, the nan() function returns a generic NaN value.

nan() Return value

The na() function returns quiet NaN value.

Example: nan() function

#include <iostream>
#include <cmath>
#include <cstring>

using namespace std;

int main()
    double src = nan("1");
    uint64_t dest;
    // copies variable src to dest
    // use <cstring> for memcpy()
    memcpy(&dest, &src, sizeof src);
    cout << "nan(\"1\") = " << src << " (" << hex << dest << ")\n";

    return 0;

When you run the program, the output will be:

nan("1") = nan (7ff8000000000001)
Did you find this article helpful?