Xref: utzoo comp.unix.ultrix:7438 comp.lang.fortran:5556 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!interet!jim From: jim@interet.UUCP (User) Newsgroups: comp.unix.ultrix,comp.lang.fortran Subject: Re: How to detect NaN's Summary: simple test for non-ordinary numbers Message-ID: <15@interet.UUCP> Date: 31 May 91 13:16:46 GMT References: <1991May30.204332.16506@litwin.com> Organization: Interet, Maplewood, NJ Lines: 23 In article <1991May30.204332.16506@litwin.com>, vlr@litwin.com (Vic Rice) writes: > How can I test a variable for the presence of a NaN or Infinity ??? We test for NAN and INF by using the following: if(x*0.0 .ne. 0.0) write(*,*) 'ERROR: X is not a number' The idea is that if x is NAN, then x times zero is also NAN and not zero. Unfortunately, the above code may be optimized out by an optimizing com- piler which "knows" that zero times x is zero, so you may have to code it as: if(notone.eq.1) then temp=1.0 else c this branch is always taken temp=0.0 endif if(x*temp.ne.0.0) write(*,*) 'ERROR: X is not a number' Note that x may be NAN or maybe infinity or some other special bit pattern depending on your floating point implementation. But if the test succeeds, then X is definitely not an ordinary number. Jim uunet!interet!jim