Python sum() returns negative value because the sum is too large for 32bit integer

人走茶凉 提交于 2020-01-14 14:47:31

问题


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 converting it to long integer?

Here is my import list:

import math, csv, sys, re, time, datetime, pickle, os, gzip
from numpy import *

回答1:


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.int128 dtype 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...




回答2:


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

>>> sum(xrange(10**7))
49999995000000L
>>> from numpy import sum
>>> 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

print type(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



回答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.



来源:https://stackoverflow.com/questions/12715750/python-sum-returns-negative-value-because-the-sum-is-too-large-for-32bit-integ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!