Set how floating-point errors in the results of arithmetic operations are handled.
The options for handling floating-point errors are:
Treatment | Action |
---|---|
'ignore' | Take no action. Allows invalid values to occur in the result data array. |
'warn' | Print a RuntimeWarning (via the Python warnings module). Allows invalid values to occur in the result data array. |
'raise' | Raise a FloatingPointError exception. |
The different types of floating-point errors are:
Error | Description | Default treatment |
---|---|---|
Division by zero | Infinite result obtained from finite numbers. | 'warn' |
Overflow | Result too large to be expressed. | 'warn' |
Invalid operation | Result is not an expressible number, typically indicates that a NaN was produced. | 'warn' |
Underflow | Result so close to zero that some precision was lost. | 'ignore' |
Note that operations on integer scalar types (such as int16) are handled like floating point, and are affected by these settings.
If called without any arguments then the current behaviour is returned.
Parameters : |
|
---|---|
Returns : |
|
See also
Examples
Set treatment for all types of floating-point errors to 'raise' and then reset to the previous behaviours:
>>> cf.Data.seterr()
{'divide': 'warn', 'invalid': 'warn', 'over': 'warn', 'under': 'ignore'}
>>> old = cf.Data.seterr('raise')
>>> cf.Data.seterr(**old)
{'divide': 'raise', 'invalid': 'raise', 'over': 'raise', 'under': 'raise'}
>>> cf.Data.seterr()
{'divide': 'warn', 'invalid': 'warn', 'over': 'warn', 'under': 'ignore'}
Set the treatment of division by zero to 'ignore' and overflow to 'warn' without changing the treatment of underflow and invalid operation:
>>> cf.Data.seterr(divide='ignore', over='warn')
{'divide': 'warn', 'invalid': 'warn', 'over': 'warn', 'under': 'ignore'}
>>> cf.Data.seterr()
{'divide': 'ignore', 'invalid': 'warn', 'over': 'ignore', 'under': 'ignore'}
Some examples with data arrays:
>>> d = cf.Data([0., 1])
>>> e = cf.Data([1., 2])
>>> old = cf.Data.seterr('ignore')
>>> e/d
<CF Data: [inf, 2.0] >
>>> e**12345
<CF Data: [1.0, inf] >
>>> cf.Data.seterr(divide='warn')
{'divide': 'ignore', 'invalid': 'ignore', 'over': 'ignore', 'under': 'ignore'}
>>> e/d
RuntimeWarning: divide by zero encountered in divide
<CF Data: [inf, 2.0] >
>>> e**12345
<CF Data: [1.0, inf] >
>>> old = cf.Data.mask_fpe(False)
>>> cf.Data.seterr(over='raise')
{'divide': 'warn', 'invalid': 'ignore', 'over': 'ignore', 'under': 'ignore'}
>>> e/d
RuntimeWarning: divide by zero encountered in divide
<CF Data: [inf, 2.0] >
>>> e**12345
FloatingPointError: overflow encountered in power
>>> cf.Data.mask_fpe(True)
False
>>> cf.Data.seterr(divide='ignore')
{'divide': 'warn', 'invalid': 'ignore', 'over': 'raise', 'under': 'ignore'}
>>> e/d
<CF Data: [inf, 2.0] >
>>> e**12345
<CF Data: [1.0, --] >