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