NaN's (was Re: FLOATING NULL?)

Walter Bright bright at nazgul.UUCP
Tue Jun 4 04:06:27 AEST 1991


In article <14 at tdatirv.UUCP> sarima at tdatirv.UUCP (Stanley Friesen) writes:
/>In article <2726 at root44.co.uk> gwc at root.co.uk (Geoff Clare) writes:
/>I agree there is no standard way of generating NaN (although sqrt(-1.0)
/>is your best bet).  However, there is a standard way of testing for NaN.
/>If x is a floating point variable, then (x != x) will be TRUE if and
/>only if x has the value NaN.
/But what if the compiler notices you are asking for a self comparison and
/optimizes it away?  Then this will fail unless x is volatile.

If the compiler supports NaNs, it will not do such optimizations. Note that
there is a proposed standard for generating NaNs, it is put out by the
NCEG (Numerical C Extensions Group). The idea is that there is a NAN macro,
which generates a quiet NaN, and a NANS macro, which generates a signalling
NaN. Both can be used for static initialization of floating point variables.
(Which is impractical if you are using sqrt(-1) to generate a NaN.)

Many C implementations claim to be IEEE 748 conformant, but if you try
the (x!=x) test above with x being a NaN, you'll find that they aren't
very conformant. (x!=x) not only is supposed to return TRUE, but also does
not raise the invalid exception when x is a NaN. This is where most
implementations fall down, including some Fortrans!

Zortech C/C++ 3.0 is the first implementation of NCEG and IEEE 748 for C
that I'm aware of. It includes full support for programming with NaNs.
It also includes all 26 IEEE 748 comparison operators!

If you'd like more information, please email me a note and I'll email you
a brochure.



More information about the Comp.lang.c mailing list