CHECK_MATH

The CHECK_MATH function returns and clears the accumulated math error status. The returned value is the sum of the bit values (described in Math Error Status Values (note that not all machines detect all errors) below) of the accumulated errors.

  • Math Error Status Values (note that not all machines detect all errors)

Value

Condition

0

No errors detected since the last interactive prompt or call to CHECK_MATH

1

Integer divided by zero

2

Integer overflow

16

Floating-point divided by zero

32

Floating-point underflow

64

Floating-point overflow

128

Floating-point operand error. An illegal operand was encountered, such as a negative operand to the SQRT or ALOG functions, or an attempt to convert to integer a number whose absolute value is greater than 2 31 - 1

Note that each type of error is only represented once in the return value--any number of "Integer divided by zero" errors will result in a return value of 1.

The math error status is cleared (reset to zero) when CHECK_MATH is called, or when errors are reported. Math errors are reported either never, when the interpreter returns to an interactive prompt, or after execution of each IDL statement, depending on the value of the !EXCEPT system variable (see ). See "Examples," below, for further discussion.

Calling Sequence

Result = CHECK_MATH(   )

Keywords

PRINT

Set this keyword to print an error message to the IDL command log if any accumulated math errors exist. If this keyword is not present, CHECK_MATH executes silently.

Examples

To simply check and clear the accumulated math error status using all the defaults, enter:

PRINT, CHECK_MATH()

IDL prints the accumulated math error status code and resets to zero.

CHECK_MATH and !EXCEPT

Because the accumulated math error status is cleared when it is reported, the behavior and appropriate use of CHECK_MATH depends on the value of the system variable !EXCEPT.

For example:

!EXCEPT=0 ; Set value of !EXCEPT to zero.

PRINT, 1./0., 1/0 ; Both of these operations cause errors.

IDL prints:

Inf    1 ; The special floating-point value Inf; is returned for 1./0.; There is no integer analogue to the floating-point Inf; .

PRINT, CHECK_MATH() ; Check the accumulated error status .

IDL prints:

    17 ; CHECK_MATH reports floating-point and integer divide-by-zero errors.

!EXCEPT=1 ; Set value of !EXCEPT to one.

PRINT, 1./0., 1/0 ; Both of these operations cause errors.

IDL prints:

Inf    1 ; This time IDL also prints error messages.

% Program caused arithmetic error: Integer divide by 0

% Program caused arithmetic error: Floating divide by 0

PRINT, CHECK_MATH() ; Check the accumulated error status .

IDL prints:

    0 ; Status was reset.

However, if we do not allow IDL to return to an interactive prompt before checking the math error status:

!EXCEPT=1 ; Set value of !EXCEPT to one.

PRINT, 1./0., 1/0 & PRINT, CHECK_MATH()
; Call to CHECK_MATH happens before returning to the IDL command prompt.

IDL prints:

Inf    1

17

In this case, the math error status code (17) is printed, but because the error status has been cleared by the call to CHECK_MATH, no error messages are printed when IDL returns to the interactive command prompt. Finally,

!EXCEPT=2 ; Set value of !EXCEPT to two.

PRINT, 1./0., 1/0 & PRINT, CHECK_MATH()
; Call to CHECK_MATH happens before returning to the IDL command prompt.

IDL prints:

Inf 1

% Program caused arithmetic error: Integer divide by 0

% Program caused arithmetic error: Floating divide by 0

% Detected at $MAIN$

0

Errors are printed before executing the CHECK_MATH function, so CHECK_MATH reports no errors. However, if we include the call to CHECK_MATH in the first PRINT command, we see the following:

PRINT, 1./0., 1/0, CHECK_MATH(); Call to CHECK_MATH is part of a single IDL statement.

IDL prints:

Inf    1    17

Printing Error Messages

The following code fragment prints abbreviated names of errors that have occurred:

ERRS = ['Divide by 0', 'Underflow', 'Overflow', $

        'Illegal Operand'] ; Create a string array of error names.

J = CHECK_MATH() ; Get math error status.

FOR I = 4, 7 DO IF ISHFT(J, -I) AND 1 THEN $

  PRINT, ERRS(I-4), ' Occurred' ; Check to see if an error occurred and print the corresponding error message.

Testing Critical Code

Assume you have a critical section of code that is likely to produce an error. The following example shows how to check for errors, and if one is detected, how to repeat the code with different parameters.

JUNK = CHECK_MATH(/PRINT) ; Clear error status from previous operations, and print error messages if an error exists.

!EXCEPT=0 ; Disable automatic printing of subsequent math errors.

AGAIN: ... ; Critical section goes here.

IF CHECK_MATH() NE 0 THEN BEGIN ; Did an arithmetic error occur? If so, print error message and request new values.

PRINT, 'Math error occurred in critical section.'

READ, 'Enter new values.',... ; Get new parameters from user.

!EXCEPT=2 ; Enable automatic printing of math errors.

GOTO, AGAIN ; And retry.

ENDIF

See Also

FINITE , ISHFT , MACHAR , , , and Math Errors .