nlcpy.errstate

class nlcpy.errstate(*args, **kwargs)[source]

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 upon exiting it is reset to what it was before.

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’, ‘print’}.

See also

seterr

Sets how floating-point errors are handled.

geterr

Gets the current way of handling floating-point errors.

Examples

>>> import nlcpy as vp
>>> olderr = vp.seterr(all='ignore')  # Set error handling to known state.
>>> vp.arange(3) / 0.
array([nan, inf, inf])
>>> with vp.errstate(divide='warn'):
...     vp.arange(3) / 0.  
<stdin>:2: RuntimeWarning: divide by zero encountered in any of (nlcpy_arange, nlcpy_true_divide)
array([nan, inf, inf])
>>> vp.sqrt(-1)
array(nan)
>>> with vp.errstate(invalid='raise'):  
...     vp.sqrt(-1)
Traceback (most recent call last):
...
FloatingPointError: invalid value encountered in (nlcpy_sqrt)

Outside the context the error handling behavior has not changed:

>>> vp.geterr()
{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}
>>> _ = vp.seterr(**olderr)

Methods

__call__(func)

Call self as a function.

__enter__()[source]
__exit__(*exc_info)[source]
__eq__(value, /)

Return self==value.

__ne__(value, /)

Return self!=value.

__lt__(value, /)

Return self<value.

__le__(value, /)

Return self<=value.

__gt__(value, /)

Return self>value.

__ge__(value, /)

Return self>=value.