[IPython-dev] from __future__ import division

Fernando Perez fperez.net at gmail.com
Thu Jun 15 03:46:04 CDT 2006


On 6/15/06, Fernando Perez <fperez.net at gmail.com> wrote:
> On 6/15/06, Gaël Varoquaux <gael.varoquaux at normalesup.org> wrote:

> >     If I start ipython with no switch, this works fine. If I start it
> > with "-pylab" switch", then I get an "OverflowError: math range error" on
> > the last line. This is a minimal example. Removing "w_O" from the
> > definition of "w" gets rid of the error.

OK, here's your problem:

In [2]: xmode
Exception reporting mode: Verbose

In [3]: run divbug
2/3 is not 0!  0.666666666667

scipy version: 0.5.0.1940
mpl   version: 0.87.3
numerix flag : Numeric
---------------------------------------------------------------------------
exceptions.OverflowError                             Traceback (most
recent call last)

/home/fperez/test/divbug.py
     27 [Xgrid,Ygrid] = S.mgrid[-xrange:(xrange+dx):dx,-yrange:(yrange+dy):dy]
     28
---> 29 GaussianMap = Gaussian_beam(Ygrid,0)
        GaussianMap = undefined
        Gaussian_beam = <function Gaussian_beam at 0x42741a74>
        Ygrid = array([[-0.002  , -0.00196, -0.00192, ...,  0.00192,
0.00196,  0.002  ],
       [-0.002  , -0.00196, -0.00192, ...,  0.00192,  0.00196,  0.002  ],
       [-0.002  , -0.00196, -0.00192, ...,  0.00192,  0.00196,  0.002  ],
       ...,
       [-0.002  , -0.00196, -0.00192, ...,  0.00192,  0.00196,  0.002  ],
       [-0.002  , -0.00196, -0.00192, ...,  0.00192,  0.00196,  0.002  ],
       [-0.002  , -0.00196, -0.00192, ...,  0.00192,  0.00196,  0.002  ]])
     30
     31 print

/home/fperez/test/divbug.py in Gaussian_beam(y=array([[-0.002  ,
-0.00196, -0.00192, ...,  0.00...6, -0.00192, ...,  0.00192,  0.00196,
 0.002  ]]), z=0)
     16 def Gaussian_beam(y,z):
     17     wz = w(z)
---> 18     out = P.exp(-(y**2)/(2*wz**2))
        out = undefined
        global P.exp = <ufunc 'exp'>
        y = array([[-0.002  , -0.00196, -0.00192, ...,  0.00192,
0.00196,  0.002  ],
       [-0.002  , -0.00196, -0.00192, ...,  0.00192,  0.00196,  0.002  ],
       [-0.002  , -0.00196, -0.00192, ...,  0.00192,  0.00196,  0.002  ],
       ...,
       [-0.002  , -0.00196, -0.00192, ...,  0.00192,  0.00196,  0.002  ],
       [-0.002  , -0.00196, -0.00192, ...,  0.00192,  0.00196,  0.002  ],
       [-0.002  , -0.00196, -0.00192, ...,  0.00192,  0.00196,  0.002  ]])
        wz = 5.0000000000000002e-05
     19     return out
     20

OverflowError: math range error
WARNING: Failure executing file: <divbug.py>


It appears when you use the 'Numeric' value for numerix in pylab:
NUmeric didn't handle underflow very gracefully.  The smallest value
in the argument to P.exp()  is -800:

ipdb> aa=-(y**2)/(2*wz**2)
ipdb> bb=P.reshape(aa,(101*101,))
ipdb> P.amin(bb)
-800.0
ipdb> P.exp(-800)
*** OverflowError: math range error

You can see that this is the actual problem in a direct manner:

In [1]: import numpy

In [2]: import Numeric

In [3]: numpy.exp(-800)
Out[3]: 0.0

In [4]: Numeric.exp(-800)
---------------------------------------------------------------------------
exceptions.OverflowError                             Traceback (most
recent call last)

/home/fperez/test/<ipython console>

OverflowError: math range error


Since Numeric is unmaintained, you'll need to either switch to numpy,
or mask its exp function with a 'safe' one which underflows (IPython
provides one, in IPython.numutils.exp_safe precisely for this
purpose).

Cheers,

f




More information about the IPython-dev mailing list