Ryan Rusty
14
Q:

nan c++ example

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
    double nanValue;
    
    //generating generic NaN value
    //by passing an empty string
    nanValue = nan("");
    
    //printing the value 
    cout<<"nanValue: "<<nanValue<<endl;
    
    return 0;
}
1
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.
  
  // C++ code to demonstrate NaN exception 
#include<iostream> 
#include<cmath> // for sqrt() 
using namespace std; 
int main() 
{ 
    float a = 2, b = -2; 
  
    // Prints the number (1.41421) 
    cout << sqrt(a) << endl; 
  
    // Prints "nan" exception 
    // sqrt(-2) is complex number 
    cout << sqrt(b) << endl; 
  
    return 0; 
} 
Output:

1.41421
-nan
2

New to Communities?

Join the community