Path: utzoo!attcan!uunet!jarthur!usc!zaphod.mps.ohio-state.edu!mips!hal!mark From: mark@mips.COM (Mark G. Johnson) Newsgroups: comp.unix.ultrix Subject: Re: Determining IEEE NaN and INF on RISC Ultrix Message-ID: <41681@mips.mips.COM> Date: 22 Sep 90 14:39:03 GMT References: <59582@bbn.BBN.COM> Sender: news@mips.COM Reply-To: mark@mips.COM (Mark G. Johnson) Lines: 84 In article <59582@bbn.BBN.COM> fkittred@spca.bbn.com (Fletcher Kittredge) writes: > >I need to determine whether a double precision floating point number is >a IEEE NaN or INF (+ or -) value on Sun, HP and DEC RISC systems. Sun You could resort to coding your own routines. Just as an simple-simon demonstration of this possibility, the following hack was thrown together in 3 minutes. It was tested on the DECstation 3100 and works under compiler optimization levels 0, 1, 2, and 3. These codes are far from optimum, but they give a flavor of how you might proceed. Flame if you must, keeping in mind that the goal below is getting an answer, not efficiency. The basic ideas are (1) if you compare IEEE NaN with anything, the comparison fails. Even a==a fails if a is NaN. (2) Infinity is what you get when you overflow a computation. ----------------------------------------------------------------------------- #include #include /* test if argument is IEEE double precision NaN */ int inn(a) double a; { if(a==a) return(0) ; else return(1); } /* test if argument is IEEE double precision Infinity (+ or -) */ int iff(b) double b; { double c, d; /* laboriously create +Infinity and -Infinity */ c = 1e10; c = (c*c); c = (c*c); c = (c*c); c = (c*c); c = (c*c); c = (c*c); d = -1.0 *c; if((b==c) || (b==d)) return(1); else return(0); } main() { double x, y, z, q, v ; /* set z equal to +Infinity */ z = 1e10 ; z = z*z ; z = z*z ; z = z*z ; z = z*z ; z = z*z ; z = z*z ; z = z*z ; z = z*z ; /* set y equal to NaN */ y = z/z ; /* set x equal to 1.0 */ x = 1.0 ; printf(" Is %le a NAN? answer: %d \n", x, inn(x)); printf(" Is %le a NAN? answer: %d \n", y, inn(y)); printf(" Is %le a NAN? answer: %d \n", z, inn(z)); printf(" Is %le an infinity? answer: %d \n", x, iff(x)); printf(" Is %le an infinity? answer: %d \n", y, iff(y)); printf(" Is %le an infinity? answer: %d \n", z, iff(z)); } -- -- Mark Johnson MIPS Computer Systems, 930 E. Arques M/S 2-02, Sunnyvale, CA 94086 (408) 524-8308 mark@mips.com {or ...!decwrl!mips!mark}