The following program should print "Got a NaN": > gcc-4.0.2 nan.c && ./a.out Got a NaN ...but... > gcc-4.0.2 -ffast-math nan.c && ./a.out Ugh! I don't think that is proper behaviour even for -ffast-math. This came up because someone compiled Gnumeric with -ffast-math. It seems that this option is thought of as "make my program faster" with no mention of its meaning-altering effects by lots and lots of people out there. #include <math.h> #include <stdio.h> int main (int argc, char **argv) { double d; if (sscanf ("NaN", "%lf", &d) != 1) d = 0; if (isnan (d)) printf ("Got a NaN\n"); else printf ("Ugh!\n"); return 0; }
Created attachment 10732 [details] Output from "gcc-4.0.2 -E -ffast-math nan.c" > gcc-4.0.2 -v Using built-in specs. Target: i686-pc-linux-gnu Configured with: ../../gcc-4.0.2/configure --enable-languages=c,c++,f95 --prefix=/usr/local/products/gcc/4.0.2 --with-gnu-as --with-as=/usr/local/products/gcc/binutils-2.16/bin/as --with-gnu-ld --with-ld=/usr/local/products/gcc/binutils-2.16/bin/ld Thread model: posix gcc version 4.0.2
Please read the docs about -ffast-math. It enables the option -ffinite-math-only which means no NaNs.
-ffast-math Sets -fno-math-errno, -funsafe-math-optimizations, -fno-trapping-math, -ffinite-math-only, -fno-rounding-math, -fno-signaling-nans and fcx-limited-range. This option causes the preprocessor macro __FAST_MATH__ to be defined. This option should never be turned on by any -O option since it can result in incorrect output for programs which depend on an exact implementation of IEEE or ISO rules/specifications for math functions. -ffinite-math-only Allow optimizations for floating-point arithmetic that assume that arguments and results are not NaNs or +-Infs. This option should never be turned on by any -O option since it can result in incorrect output for programs which depend on an exact implementation of IEEE or ISO rules/specifications. The default is -fno-finite-math-only.
ok, thanks. I'll prevent this by checking for __FAST_MATH__ in my code. One could argue for a warning like nan.c:13: warning: comparison is always false due to limited range of data type for use of isnan and similarly (with true) for finite.
*** Bug 50724 has been marked as a duplicate of this bug. ***
*** Bug 60237 has been marked as a duplicate of this bug. ***