flopscope.

flopscope.numpy.errstate

flopscope.numpy.errstate(*, call=<numpy._core._ufunc_config._unspecified object at 0x7f223bace810>, all=None, divide=None, over=None, under=None, invalid=None)

Context manager for floating-point error handling.

Using an instance of errstate as a context manager allows statements in that context to execute with a known error handling behavior. Upon entering the context the error handling is set with seterr and seterrcall, and upon exiting it is reset to what it was before.

Changed in version 1.17.0.
Changed in version 2.0.

Parameters

kwargs:{divide, over, under, invalid}

Keyword arguments. The valid keywords are the possible floating-point exceptions. Each keyword should have a string value that defines the treatment for the particular error. Possible values are {'ignore', 'warn', 'raise', 'call', 'print', 'log'}.

See also

Notes

For complete documentation of the types of floating-point exceptions and treatment options, see seterr.

Examples

>>> import flopscope.numpy as fnp
>>> olderr = flops.seterr(all='ignore')  # Set error handling to known state.
>>> flops.arange(3) / 0.
array([nan, inf, inf])
>>> with flops.errstate(divide='ignore'):
... flops.arange(3) / 0.
array([nan, inf, inf])
>>> flops.sqrt(-1)
flops.float64(nan)
>>> with flops.errstate(invalid='raise'):
... flops.sqrt(-1)
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
FloatingPointError: invalid value encountered in sqrt

Outside the context the error handling behavior has not changed:

>>> flops.geterr()
{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}
>>> olderr = flops.seterr(**olderr)  # restore original state