Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!dali.cs.montana.edu!milton!sumax!polari!rwing!fnx!nazgul!bright From: bright@nazgul.UUCP (Walter Bright) Newsgroups: comp.lang.c Subject: Re: NaN's (was Re: FLOATING NULL?) Message-ID: <339@nazgul.UUCP> Date: 3 Jun 91 18:06:27 GMT References: <13223@uhccux.uhcc.Hawaii.Edu> <1991May28.153655.24199@zoo.toronto.edu> <2726@root44.co.uk> <14@tdatirv.UUCP> Reply-To: bright@nazgul.UUCP (Walter Bright) Organization: Zortech, Seattle Lines: 28 In article <14@tdatirv.UUCP> sarima@tdatirv.UUCP (Stanley Friesen) writes: />In article <2726@root44.co.uk> gwc@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.