Skip to content Skip to sidebar Skip to footer

Python Sum() Returns Negative Value Because The Sum Is Too Large For 32bit Integer

x = [1, 2, 3, ... ] y = sum(x) The sum of x is 2165496761, which is larger than the limit of 32bit integer So sum(x) returns -2129470535. How can I get the correct value by conver

Solution 1:

Twenty quatloos says you're using numpy's sum function:

>>>sum(xrange(10**7))
49999995000000L
>>>from numpy importsum>>>sum(xrange(10**7))
-2014260032

So I'd bet you did from numpy import * or are using some interface which does the equivalent.

To verify this, try

printtype(sum(x))

On the example posted elsewhere in this thread:

>>> sum([721832253, 721832254, 721832254])
-2129470535>>> type(sum([721832253, 721832254, 721832254]))
<type'numpy.int32'>

Edit: somebody owes me twenty quatloos! Either don't use the star import (best), manually set the dtype:

>>>sum([721832253, 721832254, 721832254],dtype=object)
2165496761L

or refer to the builtin sum explicitly (possibly giving it a more convenient binding):

>>>__builtins__.sum([721832253, 721832254, 721832254])
2165496761L

Solution 2:

The reason why you get this invalid value is that you're using np.sum on a int32. Nothing prevents you from not using a np.int32 but a np.int64 or np.int128dtype to represent your data. You could for example just use

x.view(np.int64).sum()

On a side note, please make sure that you never use from numpy import *. It's a terrible practice and a habit you must get rid of as soon as possible. When you use the from ... import *, you might be overwriting some Python built-ins which makes it very difficult to debug. Typical example, your overwriting of functions like sum or max...

Solution 3:

Python handles large numbers with arbitrary precision:

>>>sum([721832253, 721832254, 721832254])
2165496761

Just sum them up!

To make sure you don't use numpy.sum, try __builtins__.sum() instead.

Post a Comment for "Python Sum() Returns Negative Value Because The Sum Is Too Large For 32bit Integer"