How to detect NaN's

User jim at interet.UUCP
Fri May 31 23:16:46 AEST 1991


In article <1991May30.204332.16506 at litwin.com>, vlr at 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



More information about the Comp.unix.ultrix mailing list