Confident OP's code does not always work.
A double
(e.g. IEEE 754) has a non-unique NaN binary representation of:
(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 it is INF
.) The difference between quiet NaN and signaling NaN is often signified with the value of b
.
The salient issue is that not all NaN have the same bit pattern. The testing proposed for double
against a single bit pattern is an insufficient test for NaN detection.
Suggest instead a portable test that does not rely on NaN, nor Infinity existing in a given C's 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 fix: thanks to @Jongware