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

Keith Goodman kwgoodman at gmail.com
Tue Feb 5 23:27:46 EST 2008


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



More information about the NumPy-Discussion mailing list