Getting Rid of Underflow Errors

QUESTION: I have a section of IDL code that produces floating point underflow error messages. I realize these error messages (in my code, at least) are completely benign, but they bother the end-user. Is there a way to turn these kinds of errors off?

ANSWER: Doesn't this just drive you crazy!? Unfortunately, math error messaging is pretty much an all or nothing affair. But here is one approach to solving the problem.

Reporting of invalid mathematical computations is controlled by the !Except system variable. Normally this system variable has a value of 1, which causes IDL to report exceptions at the end of an IDL procedure or function. You can completely turn these mathematical checks off by setting !Except to 0, but doing so to get rid of the maddening underflow errors throws the baby out with the bath water because it causes all math error checking to be turned off. A risky proposition, at best.

To get around this liability, we typically turn math error checking off in our procedures and functions, but continue to test manually for mathematical errors. Ideally, you would place this code around the mathematical code that was producing the error. In short programs, or in programs where you are not sure where the error is being produced, we sometimes place the code around the entire body of the procedure or function.

First, save the current !Except value, and then set !Except to 0 to turn math error reporting off.

   currentExcept = !Except
   !Except = 0

Next, silently flush the current accumulated math error register. You do this with the Check_Math command.

   void = Check_Math()

Now comes your code. This is the code that will be generating the floating point underflow errors, whatever it is. Then, before you exit the procedure or function, you will add this code to check to see if a floating point underflow has occurred. If it has, you ignore it. If another type of math error has occurred, you report it with the Message command. Notice the floating point underflow error is represented as a value of 32 in the Check_Math documentation.

   floating_point_underflow = 32 
   status = Check_Math()         ; Get status and reset accumulated math error register.
   IF(status AND NOT floating_point_underflow) NE 0 THEN $
      Message, 'IDL Check_Math() error: ' + StrTrim(status, 2)

Finally, restore the original math error reporting condition.

   !Except = currentExcept 

Google
 
Web Coyote's Guide to IDL Programming