[SciPy-user] possible to get INF in divide by zero ?

Robert Kern robert.kern at gmail.com
Thu Aug 28 04:10:22 EDT 2008


On Thu, Aug 28, 2008 at 02:48, Stef Mientki <s.mientki at ru.nl> wrote:

> Thanks, so it works for arrays,
> but not for normal integers:
>  >>> 3/0
> Traceback (most recent call last):
>  File "<interactive input>", line 1, in <module>
> ZeroDivisionError: integer division or modulo by zero

Right. Python integers and floats explicitly check for this case and
raise the error. numpy objects, either arrays or numpy scalar types,
have a configurable mechanism. If you want scalars that work like
this:


In [1]: from numpy import *

In [2]: float64(1.0) / 0.0
Out[2]: inf

In [5]: seterr(divide='raise')
Out[5]: {'divide': 'ignore', 'invalid': 'ignore', 'over': 'ignore',
'under': 'ignore'}

In [6]: float64(1.0) / 0.0
---------------------------------------------------------------------------
FloatingPointError                        Traceback (most recent call last)

/Users/rkern/Downloads/Video/avy/<ipython console> in <module>()

FloatingPointError: divide by zero encountered in double_scalars

In [7]: seterr(divide='warn')
Out[7]: {'divide': 'raise', 'invalid': 'ignore', 'over': 'ignore',
'under': 'ignore'}

In [8]: float64(1.0) / 0.0
/usr/local/bin/ipython:1: RuntimeWarning: divide by zero encountered
in double_scalars
  #!/Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python
Out[8]: inf

In [9]: seterr(divide='print')
Out[9]: {'divide': 'warn', 'invalid': 'ignore', 'over': 'ignore',
'under': 'ignore'}

In [10]: float64(1.0) / 0.0
Warning: divide by zero encountered in double_scalars
Out[10]: inf

In [12]: seterr(divide='ignore')
Out[12]: {'divide': 'print', 'invalid': 'ignore', 'over': 'ignore',
'under': 'ignore'}

In [13]: float64(1.0) / 0.0
Out[13]: inf

In [14]: seterr?
Type:           function
Base Class:     <type 'function'>
String Form:    <function seterr at 0x18e8e30>
Namespace:      Interactive
File:           /Users/rkern/svn/numpy/numpy/core/numeric.py
Definition:     seterr(all=None, divide=None, over=None, under=None,
invalid=None)
Docstring:
    Set how floating-point errors are handled.

    Valid values for each type of error are the strings
    "ignore", "warn", "raise", and "call". Returns the old settings.
    If 'all' is specified, values that are not otherwise specified
    will be set to 'all', otherwise they will retain their old
    values.

    Note that operations on integer scalar types (such as int16) are
    handled like floating point, and are affected by these settings.

    Example:

    >>> seterr(over='raise') # doctest: +SKIP
    {'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore',
'under': 'ignore'}

    >>> seterr(all='warn', over='raise') # doctest: +SKIP
    {'over': 'raise', 'divide': 'ignore', 'invalid': 'ignore',
'under': 'ignore'}

    >>> int16(32000) * int16(3) # doctest: +SKIP
    Traceback (most recent call last):
          File "<stdin>", line 1, in ?
    FloatingPointError: overflow encountered in short_scalars
    >>> seterr(all='ignore') # doctest: +SKIP
    {'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore',
'under': 'ignore'}

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco



More information about the SciPy-User mailing list