[Numpy-discussion] RuntimeWarning: divide by zero encountered in log

Nathaniel Smith njs at pobox.com
Fri May 17 01:06:06 EDT 2013


On 16 May 2013 19:48, "Jonathan Helmus" <jjhelmus at gmail.com> wrote:
>
> On 05/16/2013 01:42 PM, Neal Becker wrote:
> > Is there a way to get a traceback instead of just printing the
> > line that triggered the error?
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> Neal,
>
>      Look at the numpy.seterr function.  You can use it to change how
> floating-point errors are handled, including raising a
> FloatingPointError with a traceback as opposed to printing a
> RuntimeWarning.
>
> Example
>
> $ cat foo.py
> import numpy as np
>
> np.seterr(divide='raise')
>
> a = np.array([1,1,1], dtype='float32')
> a / 0
>
> $ python foo.py
> Traceback (most recent call last):
>    File "test.py", line 6, in <module>
>      a / 0
> FloatingPointError: divide by zero encountered in divide

You also have the option of using Python's general ability to customize how
any warning is handled - see the 'warnings' module and -W switch.

If you just want a traceback printed without an exception then I think you
can do that with np.seterr too (using np.seterrcall).

-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130517/82571907/attachment.html>


More information about the NumPy-Discussion mailing list