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
) are handled like floating point, and are affected by these settings.
Parameters:all{‘ignore’, ‘warn’, ‘raise’, ‘call’, ‘print’, ‘log’}, optionalSet treatment for all types of floating-point errors at once:
ignore: Take no action when the exception occurs.
warn: Print a
(via the Python
module).
raise: Raise a
.
call: Call a function specified using the
function.
print: Print a warning directly to stdout.
log: Record error in a Log object specified by
.
The default is not to change the current behavior.
divide{‘ignore’, ‘warn’, ‘raise’, ‘call’, ‘print’, ‘log’}, optionalTreatment for division by zero.
over{‘ignore’, ‘warn’, ‘raise’, ‘call’, ‘print’, ‘log’}, optionalTreatment for floating-point overflow.
under{‘ignore’, ‘warn’, ‘raise’, ‘call’, ‘print’, ‘log’}, optionalTreatment for floating-point underflow.
invalid{‘ignore’, ‘warn’, ‘raise’, ‘call’, ‘print’, ‘log’}, optionalTreatment for invalid floating-point operation.
Returns:old_settingsdictDictionary containing the old settings.
Notes
The floating-point exceptions are defined in the IEEE 754 standard
:
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.
Concurrency note: see
Examples
>>> importnumpyasnp>>> 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'}