Is it NaN ?

C was the computer language that I seriously began programming with. Before c, I had used basic to code few games for myself. But after my first job, I began with c. Then Fortran entered my life and ever since it has dominated my work.

Recently a friend asked me to code a simple routine which checks if the number is nan or not a nan. So I dipped my feet in c. 

 

Here’s the little program i came up with lots of help and blessings of wikipedia.. 🙂

So here’s the program listing of simple isNAN function in c!!

#include <stdio.h>
void myisnan(float x);
int main()
{
    int i=0, j=0, nan_count=0;
     float x ,y;
     y=0.0;
     x = y/0.0;
     myisnan(x);
     x=y/1.0;
     myisnan(x);
}
 

void myisnan(float x)
{
int *temp;
int exp, man;
temp = (int*)&x;
exp = *temp & 0x7f800000; // Extract the exponent;
man = *temp & 0x7fffff;   // Extract the mantissa;
if ((exp == 0x7f800000) && (man != 0))
{
   printf("\n%f – We found NAN\n ",x);
}
else
{
   printf("\nHey %f – is not NAN\n",x);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s