NaN spreads when applied to a numerical operation. Thus, it is enough to verify the final result in order to be NaN. As for how to do this - if for building for> = C ++ 11 there is std :: isnan, as Goz noted. For <C ++ 11 - if you want to be bulletproof - I would personally do a bit check (especially if optimization could be involved). Sample for NaN is
? 11.......1 xx.......x sign bit ^ ^exponent^ ^fraction^
Where? can be any, and at least one x must be equal to 1.
For a platform-specific solution, there is another possibility of seams. There is a feenableexcept function in glibc (possibly using the signal function and the compiler option -fnon-call-exceptions ) that enables the generation of SIGFPE syntax when an invalid floating point operation occurs. And the _control87 function (possibly with the _set_se_translator function and the /EHa compiler /EHa ), which is almost the same in VC.
source share