Xref: utzoo comp.unix.ultrix:7758 comp.lang.fortran:5738 Newsgroups: comp.unix.ultrix,comp.lang.fortran Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!uupsi!pixar!mccoy From: mccoy@pixar.com (Dan McCoy) Subject: Re: How to detect NaN's Message-ID: <1991Jun19.235801.5775@pixar.com> Sender: news@pixar.com (Usenet Newsmaster) Nntp-Posting-Host: valkyrie Organization: Pixar -- Point Richmond, California References: <1991May30.204332.16506@litwin.com> Date: Wed, 19 Jun 1991 23:58:01 GMT 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 ??? Others have answered this question if detecting these values is what you really want. But if what you really want is to write code that won't fail when presented with NaN's and Inf's, it's often possible to just write code so that sensible behaviour just falls out. For instance, a NaN will fail any test. It's not equal to anything, including itself, and it's not greater than anything or less than anything. If you write: if (x > maxvalue) outofrange(); x=NaN will appear to be in range, since it will fail the ">" test. But if you write: if (!(x <= maxvalue)) outofrange(); then x=NaN will be out of range since it failed the "<=" test. This type of defensive coding allows you to write bullet-proof code without having to clog it up with explicit NaN checks all over the place. (I noticed the cross-post to comp.lang.fortran just before sending. The concept applies just as well in Fortran. However my Fortran is so rusty, I better let the example stand in C.) Dan McCoy Pixar ...!ucbvax!pixar!mccoy