NaN problems

The code below does not seem to conform. Namely the nan function call needs -D__STDC_VERSION__=199901L to act properly (I guess this will be fixed in 6.3). Another problem is that the NaN compares equal to the constant. This differs from gcc (although the intel compiler 9.0 seems to contain this behavior as well).

Is comparing NaN’s implementation defined? I didn’t see anything in the standard about comparing NaNs. I seem to remember something about NaN != NaN should be true, and the second program would illustrate this failing as well

[chulbert@fourier ~]$ cat test.c
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

int
main(void)
{
double x = 0.0, y = 0.0, z;

z = x / (x-z);
printf("%f,%d\n",z,z==-1);

z = nan("");
printf("%f,%d\n",z,z==-1);

z = strtod(“NAN()”,NULL);
printf("%f,%d\n",z,z==-1);
return EXIT_SUCCESS;
}
[chulbert@fourier ~]$ gcc -std=c9x test.c -lm && ./a.out
nan,0
nan,0
nan,0
[chulbert@fourier ~]$ pgcc -c9x test.c -lm && ./a.out
test.c:
nan,1
0.000000,0
nan,1
[chulbert@fourier ~]$ pgcc -c9x -D__STDC_VERSION__=199901L test.c -lm && ./a.out
test.c:
nan,1
nan,1
nan,1


[chulbert@fourier ~]$ cat test.c
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

int
main(void)
{
double x = 0.0, y = 0.0, z;

z = x / (x-y);
printf("%f,%d\n",z,z!=z);

z = nan("");
printf("%f,%d\n",z,z!=z);

z = strtod(“NAN()”,NULL);
printf("%f,%d\n",z,z!=z);
return EXIT_SUCCESS;
}
[chulbert@fourier ~]$ pgcc -c9x -D__STDC_VERSION__=199901L test.c -lm && ./a.out
test.c:
nan,0
nan,0
nan,0

Hi Chris,

I believe this behavior is defined in IEEE 754 which is not adhered to by default. To use this standard, add “-Kieee” to your compilation. For Intel, I think you need to add “-mp”. Also, if you add “-ffast-math” to gcc, it will get the same “wrong” answer.

% gcc -std=c99 -lm nan.c -ffast-math && a.out
-0.000000,0
nan,1
nan,1
% pgcc -V6.2-3 -c99 nan.c -D__STDC_VERSION__=199901L && a.out
-0.000000,0
nan,1
nan,1
% pgcc -V6.2-3 -c99 nan.c -D__STDC_VERSION__=199901L -Kieee && a.out
-0.000000,0
nan,0
nan,0

Hope this helps,
Mat

Great! I guess that flag escaped me. I didn’t even think to turn off the gcc ieee stuff. Just as a correction for future readers, the intel flag is -mieee-fp and conforms to PGI output a well.