[Numpy-discussion] Traceback on divide by zero error

Robert Kern robert.kern@gmail....
Thu Dec 13 13:21:14 CST 2007


Tony S Yu wrote:
> Hello,
> 
> This is something that's been bothering for awhile. When numpy raises
> the following divide by zero error:
>>>> Warning: divide by zero encountered in double_scalars
> is there a way to get a Traceback on where that warning occurred.

In [1]: from numpy import *

In [2]: seterr?
Type:             function
Base Class:       <type 'function'>
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'}

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

In [4]: ones(10) / zeros(10)
---------------------------------------------------------------------------
FloatingPointError                        Traceback (most recent call last)

/Users/rkern/svn/mpl-toolkits/<ipython console> in <module>()

FloatingPointError: divide by zero encountered in divide

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

In [6]: ones(10) / zeros(10)
Out[6]: array([ Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf])


-- 
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 Numpy-discussion mailing list