3
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 *
4

3 に答える 3

14

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
于 2012-10-03T19:51:56.720 に答える
3

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

于 2012-10-03T20:49:53.753 に答える
2

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.

于 2012-10-03T19:50:24.767 に答える