# [SciPy-user] Divide by zero is inf?

Ryan Krauss ryanlists at gmail.com
Mon Dec 12 10:32:03 CST 2005

```Use arctan2:

In [50]: arctan2(1,0)
Out[50]: 1.5707963267948966

On 12/12/05, Alexander Borghgraef <alexander.borghgraef.rma at gmail.com> wrote:
> Hi all,
>
>   I'm trying to convert a field of 2Dvectors (3D-array) from Cartesian to
> pole coordinates. Now I would like to use as
>  few for loops as possible, and use ufuncs instead. However, in calculating
> the angle coordinate via the formula
>  "theta = arctan(y/x)", there is a risk of division by zero. In a for-loop
> scheme, this is easily solved by adding a test inside
>  the loop, checking for x == 0, but there's no such thing for a ufunc
> scheme.
>   Now, I am aware of the existence of "inf" in scipy, and  since
> "arctan(inf) == pi /2", there should be no problem with the
>  following code:
>
>
> vectorField = array( [ zeros( [10,10] ), ones( [10,10] ) ] )
>
> poleField = array( [ sqrt( sum( vectorField **2 ) ), arctan( vectorField[ 1,
> :, :] / vectorField[ 0, :, :]  ) ] )
>
>  However, I can't seem to find out how to do division by zero resulting in
> "inf", I always get a "ZeroDivisionError"
>  exception. How do I do this? Any other suggestions of solving similar
> problems with ufuncs? I'm currently using scipy 0.3.2,
>  with Numeric 24.1.
>
>
>  --
> Alex Borghgraef
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>
>
>

```