Popular Tutorials
Start Learning C++Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
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.
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?