With -ffast-math, isnan should return true if passed a NaN value. Otherwise, how is isnan different than (x!=x) ? isnan worked as expected with gcc 4.7, but does not with 4.8.1 and 4.8.2 How can I check if x is a NaN in a portable way (not presuming any compilation option) ?
Well, -ffast-math implies -ffinite-math-only, so the compiler is assuming no NaNs or infinites are used as arguments/return values of any expression. So, if you have a program that produces NaNs anyway, you shouldn't be building it with -ffast-math, at least not with -ffinite-math-only.
Thank you for your answer. My program (which is a computational fluid dynamics solver) is not supposed to produce NaNs. However, when it does (which means something went wrong), I would like to abort the program and return an error instead of continuing crunching NaNs. I also want it to run as fast as possible (hence the -ffast-math option). I would argue that: if printf("%f",x) outputs "NaN", isnan(x) should also be returning true. Do you have a suggestion concerning my last question: How can I check if x is NaN in a portable way (not presuming any compilation option) ?
(In reply to N Schaeffer from comment #2) > Do you have a suggestion concerning my last question: > How can I check if x is NaN in a portable way (not presuming any compilation > option) ? This should bypass software optimizations. But if the hardware is put in a mode that does strange things with NaN, it will be harder to work around. int my_isnan(double x){ volatile double y=x; return y!=y; }
int my_isnan(double x){ volatile double y=x; return y!=y; } is translated to: 0x0000000000406cf0 <+0>: movsd QWORD PTR [rsp-0x8],xmm0 0x0000000000406cf6 <+6>: xor eax,eax 0x0000000000406cf8 <+8>: movsd xmm1,QWORD PTR [rsp-0x8] 0x0000000000406cfe <+14>: movsd xmm0,QWORD PTR [rsp-0x8] 0x0000000000406d04 <+20>: comisd xmm1,xmm0 0x0000000000406d08 <+24>: setne al 0x0000000000406d0b <+27>: ret which also fails to detect NaN, which is right according to the documented behaviour of comisd: http://www.jaist.ac.jp/iscenter-new/mpc/altix/altixdata/opt/intel/vtune/doc/users_guide/mergedProjects/analyzer_ec/mergedProjects/reference_olh/mergedProjects/instructions/instruct32_hh/vc44.htm
Dup. You can try building with -fno-builtin-isnan, but no - that's not a "portable" way. *** This bug has been marked as a duplicate of bug 25975 ***
-fno-builtin-isnan is also interesting, thanks. Is there somewhere a rationale for not making isnan() find NaN's with -ffinite-math-only ?
(In reply to N Schaeffer from comment #6) > Is there somewhere a rationale for not making isnan() find NaN's with > -ffinite-math-only ? finite-math-only is basically a promise that isinf and isnan always return false... I have inline library functions that do: if (isnan(x)) ... When compiled with -ffinite-math-only, I want this eliminated as dead code, that's the reason I made the function inline in the first place. Another way to cheat: int myisnan(double)__asm__("isnan");