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.
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.
To simply check and clear the accumulated math error status using all the defaults, enter:
IDL prints the accumulated math error status code and resets to zero.
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.
!EXCEPT=0 ; Set value of !EXCEPT to zero.
PRINT, 1./0., 1/0 ; Both of these operations cause errors.
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 .
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.
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 .
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.
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.
% Program caused arithmetic error: Integer divide by 0
% Program caused arithmetic error: Floating divide by 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.
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.
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.
FINITE , ISHFT , MACHAR , , , and Math Errors .