Problem
The C compiler results in a 'true' response on comparison of a
NaN with an arbitrary float value. The following example code
shows the current behavior of cc(1):#include <ieeefp.h>
#include <stdio.h>main()
{
float f;/* make f a NaN */ *(long *)&f=0xffc00000L; fpsetmask(FP_X_DZ);
printf("f: %f\n", f);if ( f == 1.0 ) printf("Comparison f == 1.0 is true\n"); else printf("Comparison f == 1.0 is false\n");
}The output of the above program is:
$ a.out
f: -nan
Comparison f == 1.0 is true Cause
Although this is outside the spec of ANSI C, the IEEE and other bodies recommend the result of such a comparison to be 'false'. SCO engineering is currently improving the C compilation system to meet these recommendations. These improvements will be included in a future release of the product.
Solution
If you need to compare floating point values and are not sure if a floating point variable is infinity or NaN, then use the isnan() macro prior to the comparison, as in the example below.
#include <ieeefp.h>
#include <stdio.h>main()
{
float f;/* make f a NaN */ *(long *)&f=0xffc00000L; fpsetmask(FP_X_DZ);
printf("f: %f\n", f);if ( !isnan(f) && f == 1.0 )
printf("Comparison f == 1.0 is true\n");
else
printf("Comparison f == 1.0 is false\n");} See also
cc(1), cc(CP), isnan(S), ieee_functions(3BSD), fpgetround(3C),
fpgetround(S)