[Numpy-discussion] [Bug] important bug in method sum

Charles R Harris charlesr.harris@gmail....
Tue Feb 5 23:03:58 CST 2008


On Feb 5, 2008 9:27 PM, Keith Goodman <kwgoodman@gmail.com> wrote:

> On Feb 5, 2008 11:58 AM,  <humufr@yahoo.fr> wrote:
> > The problem is for an array larger than 256*256 the sum is going crazy.
> >
> > In [45]: numpy.arange(256*256)
> > Out[45]: array([    0,     1,     2, ..., 65533, 65534, 65535])
> >
> > In [46]: numpy.arange(256*256).sum()
> > Out[46]: 2147450880
> >
> > In [47]: numpy.arange(257*257)
> > Out[47]: array([    0,     1,     2, ..., 66046, 66047, 66048])
> >
> > In [48]: numpy.arange(257*257).sum()
> > Out[48]: -2113765120
>
> You hit the limit on how big an integer can be. You'll have to switch
> to floats to do the sum:
>
> >> numpy.arange(257*257, dtype=numpy.float64).sum()
> 2181202176.0



Or tell numpy to use float64 for the sum:

In [6]: a = arange(257*257)

In [7]: a.sum(dtype=float64)
Out[7]: 2181202176.0

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080205/9b51621d/attachment.html 


More information about the Numpy-discussion mailing list