C++ nan()

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.

Note: To learn more about float and double in C++, visit C++ float and double.


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?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community