c++ set to nan code example
Example 1: nan c++ example
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double nanValue;
nanValue = nan("");
cout<<"nanValue: "<<nanValue<<endl;
return 0;
}
Example 2: cpp nan value
What is NaN ?
NaN, acronym for “Not a Number” is an exception
which usually occurs in the caseswhen an expression
results in a number that can’t be represented.
For example square root of negative numbers.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float a = 2, b = -2;
cout << sqrt(a) << endl;
cout << sqrt(b) << endl;
return 0;
}
Output:
1.41421
-nan