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

Charles R Harris charlesr.harris at gmail.com
Wed Feb 6 00:03:58 EST 2008


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

> On Feb 5, 2008 11:58 AM,  <humufr at 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://mail.python.org/pipermail/numpy-discussion/attachments/20080205/9b51621d/attachment.html>


More information about the NumPy-Discussion mailing list