4.4 Handling numerical errors

By default, an error message is returned whenever calculations return values which are infinite, as in the case of 1/0, or when functions are evaluated outside the domain of parameter space in which they are defined, as in the case of besseli(-1,1). Sometimes this behaviour is desirable: it flags up to the user that a calculation has gone wrong, and exactly what the problem is. At other times, however, these error messages can be undesirable and may lead you to miss more genuine and serious errors buried in their midst.

For this reason, the issuing of explicit error messages when calculations return non-finite numeric results can be switched off by typing:

set numeric errors quiet

Having done this, expressions such as

x = besseli(-1,1)

fail silently, and variables which contain non-finite numeric results are displayed as NaN, which stands for Not a Number. The issuing of explicit errors may subsequently be re-enabled by typing:

set numeric errors explicit

Having turned off the display of numerical errors, it may be useful to use the assert command to throw an error message if a calculation has failed in an unrecoverable way that the user really ought to know about:

assert x>0 "Cannot continue with negative x"

The assert command should be followed by an algebraic expression which must be true for execution to continue. If it is false, an error results. Optionally, an error message can be included, as above, to tell the user what the problem is.