Confident OP code does not always work.
A double (for example, IEEE 754 ) has a unique binary representation of NaN:
( s is the sign bit.)
s1111111 1111baaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
Where not all baa ... aaa is 0. (if baa ... aaa is all 0, then this is INF .) The difference between quiet NaN and NaN signaling is often indicated by b .
A significant problem is that not all NaNs have the same bit pattern. The testing proposed for double for a single bit pattern is insufficient to detect NaN.
Instead, propose a portable test that does not rely on NaN, as well as an Infinity existing on this C platform.
double x; // NAN are never arithmetically equal, even to themselves. if (x != x) NaN_Detected(); if (x > DBL_MAX || x < -DBL_MAX) Inf_Detected();
[Edit] compare patch: thanks @Jongware
chux
source share