English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ nan() function usage and example

C++ Library Function <cmath>

C ++returns a double type NaN value (not a number).

The function in<cmath>Defined in the header file.

nan() prototype

double nan(const char* arg);

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

nan() parameters

Specific to the implementation C string. If the string is empty, the nan() function will return the general NaN value.

nan() return value

The na() function returns a NaN value.

Example: nan() function

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
    double src = nan("1
    uint64_t dest;
    
    //Copy the variable src to dest
    //Use <cstring> for memcpy()
    memcpy(&dest, &src, sizeof src);
    cout << "nan("1") = " << src << " (" << hex << dest << ")\n";
    return 0;
}

When the program is run, the output is:

nan("1") = nan (7ff8000000000001)

C++ Library Function <cmath>