Coyote's Guide to IDL Programming

Finding NANs with the WHERE Function

QUESTION: Consider the following array.

   array = [ 1.0, 2.0, !Values.F_NAN, 4.0, !Values.F_NAN ]

When I try to find the NANs in this array with the Where function I get this:

   Print, Where( array EQ !Values.F_NAN )
      0   1   2   3   4
   % Program caused arithmetic error: Floating illegal operand

What is going on? How can I determine where the NANs are in this array?

ANSWER: Yes. I agree this is strange, although I'm certain there is a good reason for it. The problem is that NANs are, well..., not numbers. Thus, they can't be treated as numbers in numeric expressions.

What you have to do instead is use the Finite function to determine which of the values in the array are really numbers (as opposed, for example, to not numbers). I usually check for NANs like this:

   Print, Where( NOT Float( Finite(array) ) )
      2   4

These days the code above would be more easily written like this.

   Print, Where(~Finite(array))
      2   4

You have to be just a bit careful about this, however, as Finite used as above cannot distiguish between NAN and INF (infinitites). You can use NAN, INFINITY, and SIGN keywords with Finite to distingish between the positive and negative infinities, NANs verses INFs, etc.

Google
 
Web Coyote's Guide to IDL Programming