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.
Set treatment for all types of floating-point errors at once:
RuntimeWarning (via the Python warnings module).FloatingPointError.seterrcall function.stdout.seterrcall.The default is not to change the current behavior.
Treatment for division by zero.
Treatment for floating-point overflow.
Treatment for floating-point underflow.
Treatment for invalid floating-point operation.
Dictionary containing the old settings.
See also
seterrcallSet a callback function for the ‘call’ mode.
geterr, geterrcall, errstate
The floating-point exceptions are defined in the IEEE 754 standard [1]:
Concurrency note: see Floating point error handling
>>> import numpy as np
>>> orig_settings = np.seterr(all='ignore') # seterr to known value
>>> np.int16(32000) * np.int16(3)
np.int16(30464)
>>> np.seterr(over='raise')
{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}
>>> old_settings = np.seterr(all='warn', over='raise')
>>> np.int16(32000) * np.int16(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FloatingPointError: overflow encountered in scalar multiply
>>> old_settings = np.seterr(all='print')
>>> np.geterr()
{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}
>>> np.int16(32000) * np.int16(3)
np.int16(30464)
>>> np.seterr(**orig_settings) # restore original
{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}
© 2005–2024 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/2.4/reference/generated/numpy.seterr.html