Pradyut Vatsa
Pradyut Vatsa

Reputation: 101

possible Python integer overflow error

Below is my code that I'm trying to run to solve for a maximum prioritized product problem i.e. return a maximum value for a product of two numbers in a list. My issue is that the product that is being computed is incorrect when the numbers in the input list are large. I was of the opinion that there will be no integer overflow in error and resulting product will be automatically long but that is not happening evidently as my multiplication is returning some kind of garbage.

#"python2"
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 28 17:12:38 2016

@author: pvatsa
"""
import numpy as np

n = int(raw_input())
alist = np.random.randint(100000, size = n)
print alist
assert(len(alist) == n)
alist = sorted(alist)
print alist
max_no = max(alist)
second_largest_no = alist[n-2]
print max_no*second_largest_no
#print long(product)
#print type(product)

Upvotes: 0

Views: 547

Answers (1)

val
val

Reputation: 8689

Using np.random.randint will create an array of 32 bits integers:

>>> alist = np.random.randint(100000, size = n)
>>> alist.dtype
dtype('int32')

Sorting it will preserve that type, creating a list of numpy.int32 objects instead of converting them back to Python (overflow-safe) integers:

>>> foo = sorted(alist)
>>> type(foo[-1])
<type 'numpy.int32'>

As such, the multiplication can overflow: you can solve it by either:

  • Casting your numbers back to Python numbers
  • Having an array of longs in the first place

The first case is simply a matter of converting values of interest:

>>> foo[-1] * foo[-2]
1386578402
>>> int(foo[-1]) * int(foo[-2])
9976512994L

The second can be done by calling randint with dtype=np.int64 (for numpy >= 1.11) or converting the array afterwards:

>>> llist = np.array(alist, dtype=np.int64)
>>> llist.sort()
>>> np.prod(llist[-2:])
9987503750

Upvotes: 2

Related Questions