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?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges