c++ what is nan code example

Example 1: 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;
}

Example 2: c++ double is nan

/* isnan example */
#include <stdio.h>      /* printf */
#include <math.h>       /* isnan, sqrt */

int main()
{
  printf ("isnan(0.0)       : %d\n",isnan(0.0));
  printf ("isnan(1.0/0.0)   : %d\n",isnan(1.0/0.0));
  printf ("isnan(-1.0/0.0)  : %d\n",isnan(-1.0/0.0));
  printf ("isnan(sqrt(-1.0)): %d\n",isnan(sqrt(-1.0)));
  return 0;
}

Tags:

Cpp Example