Piratetwo
Piratetwo

Reputation: 53

The long integer when using NumPy

I have the following simple codes for the sum of two vectors.

However, I get the wrong results when I use NumPy.

The results of the codes is as follows:

In [12]: %run -i test.py

The last 2 elements of the sum [7980015996L, 7992002000L]

The last 2 elements of the sum [-609918596 -597932592]

It's not a long integer, Why?

import numpy as np 
def numpysum(n):
  a = np.arange(n) ** 2 
  b = np.arange(n) ** 3 
  c = a + b

  return c

def pythonsum(n): 
  a = range(n) 
  b = range(n) 
  c = []

  for i in range(len(a)): 
      a[i] = i ** 2 
      b[i] = i ** 3 
      c.append(a[i] + b[i])

  return c

size = 2000

c = pythonsum(size)
print "The last 2 elements of the sum", c[-2:]
c = numpysum(size)
print "The last 2 elements of the sum", c[-2:]

Upvotes: 5

Views: 6398

Answers (1)

BrenBarn
BrenBarn

Reputation: 251408

Plain Python integers can grow arbitrarily large. Numpy integers cannot; they are limited by the size of the data type. If they get too big, they will wrap around and become negative. It looks like your array dtype is probably int32, which overflows and results in negative results. You can get the correct results in this case by using int64:

a = np.arange(n, dtype=np.int64) ** 2 
b = np.arange(n, dtype=np.int64) ** 3

However, it will still overflow eventually (if you make size larger). You could also use float64, which allows even larger numbers, but then you will lose precision.

The cap on integer sizes is the price you pay for the speed numpy gives you.

Upvotes: 4

Related Questions