numpy.seterr

numpy.seterr(all=None, divide=None, over=None, under=None, invalid=None)

Set how floating-point errors are handled.

Note that operations on integer scalar types (such as int16) are handled like floating point, and are affected by these settings.

Parameters:

all : {‘ignore’, ‘warn’, ‘raise’, ‘call’}, optional

Set treatment for all types of floating-point errors at once:

  • ignore: Take no action when the exception occurs
  • warn: Print a RuntimeWarning (via the Python warnings module)
  • raise: Raise a FloatingPointError
  • call: Call a function specified using the seterrcall function.

The default is not to change the current behavior.

divide : {‘ignore’, ‘warn’, ‘raise’, ‘call’}, optional

Treatment for division by zero.

over : {‘ignore’, ‘warn’, ‘raise’, ‘call’}, optional

Treatment for floating-point overflow.

under : {‘ignore’, ‘warn’, ‘raise’, ‘call’}, optional

Treatment for floating-point underflow.

invalid : {‘ignore’, ‘warn’, ‘raise’, ‘call’}, optional

Treatment for invalid floating-point operation.

Returns:

old_settings : dict

Dictionary containing the old settings.

See also

seterrcall
set a callback function for the ‘call’ mode.

geterr, geterrcall

Notes

The floating-point exceptions are defined in the IEEE 754 standard [1]:

  • Division by zero: infinite result obtained from finite numbers.
  • Overflow: result too large to be expressed.
  • Underflow: result so close to zero that some precision was lost.
  • Invalid operation: result is not an expressible number, typically indicates that a NaN was produced.
[102]http://en.wikipedia.org/wiki/IEEE_754

Examples

Set mode:

>>> seterr(over='raise') # doctest: +SKIP
{'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore',
 'under': 'ignore'}
>>> old_settings = seterr(all='warn', over='raise') # doctest: +SKIP
>>> int16(32000) * int16(3) # doctest: +SKIP
Traceback (most recent call last):
      File "<stdin>", line 1, in ?
FloatingPointError: overflow encountered in short_scalars
>>> seterr(all='ignore') # doctest: +SKIP
{'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore',
 'under': 'ignore'}

Previous topic

Floating point error handling

Next topic

numpy.geterr

This Page

Quick search