[Numpy-discussion] why not zerodivision error?

eat e.antero.tammi at gmail.com
Sun May 20 03:47:42 EDT 2012


Hi,

On Sun, May 20, 2012 at 10:21 AM, Chao YUE <chaoyuejoy at gmail.com> wrote:

> Dear all,
>
> could anybody give one sentence about this? why in the loop I didn't get
> zerodivision error by when I explicitly do this, I get a zerodivision
> error? thanks.
>
> In [7]: for i in np.arange(-10,10):
>         print 1./i
>    ...:
> -0.1
> -0.111111111111
> -0.125
> -0.142857142857
> -0.166666666667
> -0.2
> -0.25
> -0.333333333333
> -0.5
> -1.0
> inf
> 1.0
> 0.5
> 0.333333333333
> 0.25
> 0.2
> 0.166666666667
> 0.142857142857
> 0.125
> 0.111111111111
>
> In [8]: 1/0.
> ---------------------------------------------------------------------------
> ZeroDivisionError                         Traceback (most recent call last)
> /mnt/f/data/DROUGTH/<ipython-input-8-7e0bf5b37da6> in <module>()
> ----> 1 1/0.
>
> ZeroDivisionError: float division by zero
>
> In [9]: 1./0.
> ---------------------------------------------------------------------------
> ZeroDivisionError                         Traceback (most recent call last)
> /mnt/f/data/DROUGTH/<ipython-input-9-3543596c47ff> in <module>()
> ----> 1 1./0.
>
> ZeroDivisionError: float division by zero
>
> In [10]: 1./0
> ---------------------------------------------------------------------------
> ZeroDivisionError                         Traceback (most recent call last)
> /mnt/f/data/DROUGTH/<ipython-input-10-523760448f92> in <module>()
> ----> 1 1./0
>
> ZeroDivisionError: float division by zero

You may like to read more on here
http://docs.scipy.org/doc/numpy/reference/generated/numpy.seterr.html#numpy.seterr

So, for your specific example:
In []: a= arange(-10, 10)
In []: 1./ a
Out[]:
array([-0.1       , -0.11111111, -0.125     , -0.14285714, -0.16666667,
       -0.2       , -0.25      , -0.33333333, -0.5       , -1.        ,
               inf,  1.        ,  0.5       ,  0.33333333,  0.25      ,
        0.2       ,  0.16666667,  0.14285714,  0.125     ,  0.11111111])
In []: seterr(divide= 'raise')
In []: 1./ a
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
FloatingPointError: divide by zero encountered in divide


My 2 cents,
-eat

>
>
>
> Chao
>
> --
>
> ***********************************************************************************
> Chao YUE
> Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
> UMR 1572 CEA-CNRS-UVSQ
> Batiment 712 - Pe 119
> 91191 GIF Sur YVETTE Cedex
> Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
>
> ************************************************************************************
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120520/1d6d2195/attachment.html>


More information about the NumPy-Discussion mailing list