Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!sun-barr!newstop!exodus!exodus-bb!khb From: khb@chiba.Eng.Sun.COM (Keith Bierman fpgroup) Newsgroups: comp.lang.fortran Subject: Re: detecting Nan's Message-ID: Date: 11 Jan 91 03:52:25 GMT References: <9101110259.AA28451@ucbvax.Berkeley.EDU> Sender: news@exodus.Eng.Sun.COM Organization: Sun MegaSystems Lines: 74 In-reply-to: JBS@IBM.COM's message of 11 Jan 91 02:56:52 GMT In article <9101110259.AA28451@ucbvax.Berkeley.EDU> JBS@IBM.COM writes: Newsgroups: comp.lang.fortran Date: 11 Jan 91 02:56:52 GMT Sender: daemon@ucbvax.BERKELEY.EDU Lines: 5 > if(dnum*0d0 .ne. 0d0) write(*,1) >1 format(' The variable dnum is equal to NAN!') Is this correct? Suppose dnum is Inf. No, it isn't. Aside from reading the standard, let's try code! real dnum/1.0/,x/0.0/ dnum=dnum/x print*,dnum if(dnum*0d0 .ne. 0d0) print*," is nan?",dnum end f77 bork.f && a.out bork.f: MAIN: Inf is nan? Inf On some systems, vendors will have been kind and provided library functions. For example, on a Sun one might code something like real dnum/1.0/,x/0.0/ integer ir_isnan,ir_isinf ! libm function dnum=dnum/x print*,dnum if (ir_isnan(dnum)) print*,"dnum IS NaN" if (ir_isinf(dnum)) print*,"dnum IS Inf" end f77 bork.f && a.out bork.f: MAIN: Inf dnum IS Inf It would be more in keeping with the notion of standard compliance, to not treat ir_* as both a logical and an integer, so one might quite reasonably prefer to either declare ir* as logicals (despite the libm definition) or to test the value if (ir() .eq. 1) On systems which are ieee hw compliant, but have not provided the sw hooks, one can easily code these functions in whatever language seems most appropriate. To test for NaN it is probably best to use the result of the appropriate mask ANDed with the value under test. note that on a Sun, unless user overridden there will also be a message from ieee_retrospective, ala Warning: the following IEEE floating-point arithmetic exceptions occurred in this program and were never cleared: Division by Zero; Final note, if memory serves, the right mask is 0x7ff00000 cheers -- ---------------------------------------------------------------- Keith H. Bierman kbierman@Eng.Sun.COM | khb@chiba.Eng.Sun.COM SMI 2550 Garcia 12-33 | (415 336 2648) Mountain View, CA 94043