Thursday 30 May 2013

NAN (Not a Number) in C

  The header file <math.h> defines NAN and INFINITY. NAN is a constant expression of type float representing a quiet NaN. This symbolic constant is only defined if the implementation supports quiet NaNs for the float type. INFINITY is a constant expression of type float representing positive or unsigned infinity, if available; else a positive constant of type float that overflows at translation time.

The Program given below shows the working of NAN in C.

#include <math.h>
#include <stdio.h>

double sum(double in[])
{
    double out=0;
    int i;
    for (i=0; !isnan(in[i]); i++) out += in[i];
    return out;
}

int main()
{
    double list[] = {111.1, 222.2, 333.3, NAN};
    printf("sum: %E\n", sum(list));
    printf("sum: %e\n", sum((double[]){111.1, 222.2, 333.3, NAN}));
}


Click here to download the C Program.

No comments:

Post a Comment