1#include <iostream>
2#include <cmath>
3using namespace std;
4
5// main() section
6int main()
7{
8 double nanValue;
9
10 //generating generic NaN value
11 //by passing an empty string
12 nanValue = nan("");
13
14 //printing the value
15 cout<<"nanValue: "<<nanValue<<endl;
16
17 return 0;
18}
1What is NaN ?
2NaN, acronym for “Not a Number” is an exception
3which usually occurs in the caseswhen an expression
4results in a number that can’t be represented.
5 For example square root of negative numbers.
6
7 // C++ code to demonstrate NaN exception
8#include<iostream>
9#include<cmath> // for sqrt()
10using namespace std;
11int main()
12{
13 float a = 2, b = -2;
14
15 // Prints the number (1.41421)
16 cout << sqrt(a) << endl;
17
18 // Prints "nan" exception
19 // sqrt(-2) is complex number
20 cout << sqrt(b) << endl;
21
22 return 0;
23}
24Output:
25
261.41421
27-nan