flopscope.

flopscope.numpy.seterr

flopscope.numpy.seterr(all=None, divide=None, over=None, under=None, invalid=None)[flopscope source]

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', 'print', 'log'}, 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.

  • print: Print a warning directly to stdout.

  • log: Record error in a Log object specified by seterrcall.

The default is not to change the current behavior.

divide:{'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional

Treatment for division by zero.

over:{'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional

Treatment for floating-point overflow.

under:{'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional

Treatment for floating-point underflow.

invalid:{'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional

Treatment for invalid floating-point operation.

Returns

old_settings:dict

Dictionary containing the old settings.

See also

Notes

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

footnote
1

https://en.wikipedia.org/wiki/IEEE_754

Examples

>>> import flopscope.numpy as fnp
>>> orig_settings = flops.seterr(all='ignore')  # seterr to known value
>>> flops.int16(32000) * flops.int16(3)
flops.int16(30464)
>>> flops.seterr(over='raise')
{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}
>>> old_settings = flops.seterr(all='warn', over='raise')
>>> flops.int16(32000) * flops.int16(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FloatingPointError: overflow encountered in scalar multiply
>>> old_settings = flops.seterr(all='print')
>>> flops.geterr()
{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}
>>> flops.int16(32000) * flops.int16(3)
flops.int16(30464)
>>> flops.seterr(**orig_settings)  # restore original
{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}