Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!brutus.cs.uiuc.edu!apple!sun-barr!newstop!sun!urbina!nano From: nano%urbina@Sun.COM (Nano) Newsgroups: comp.sys.mac Subject: Re: Testing for NaN in Think C (help!) Message-ID: <133544@sun.Eng.Sun.COM> Date: 27 Mar 90 21:40:48 GMT References: <10538@portia.Stanford.EDU> Sender: news@sun.Eng.Sun.COM Reply-To: nano@sun.UUCP (Nano) Organization: Sun Microsystems, Mountain View Lines: 34 Melissa Hines writes: > if((test/test != 1) && (test != 0)) > ; /* Here if NaN */ > else > ; /* Here otherwise */ >Is there a simpler/ more elegant way of doing this? I think part of the >problem is that the IEEE have decreed that the result of any operation on >a NaN is a Nan. Also, I think Think C may have more than one type of NaN. I >couldn't care less about the type, I just want to know if it is a NaN. There are quiet NaN's and signalling NaN's. They differ by whether the MSBit of the fraction part of the number is 1 or 0. (Fraction as in IEEE fraction). You could always look at the actual representation of the number and determine whether it's a NaN, but the simpler way would be to compare the number to itself. The IEEE spec defines a comparison involving a NaN as "unordered", thus, if you compare test to itself, if it's a NaN, the comparison will fail: if (test != test) ; /* Here if NaN */ else ; /* Here otherwise */ I haven't tried it, but it should work this way if everything is IEEE compatible. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Fernando A. Urbina INTERNET: nano@sun.com nano@mcimail.com MCI: 408-0187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%