[Numpy-tickets] [NumPy] #492: integer overflow on plus and sum (at least)

NumPy numpy-tickets@scipy....
Thu Apr 5 17:28:24 CDT 2007


#492: integer overflow on plus and sum (at least)
------------------------+---------------------------------------------------
 Reporter:  socha       |       Owner:  somebody     
     Type:  defect      |      Status:  new          
 Priority:  normal      |   Milestone:  1.0.2 Release
Component:  numpy.core  |     Version:  none         
 Severity:  normal      |    Keywords:               
------------------------+---------------------------------------------------
 There are several places (at least) where integer operations overflow,
 instead of upgrading the size of the result to accommodate larger values.
 Here are some examples.

 {{{
 C:\Documents and Settings\socha>python
 Python 2.4.3 - Enthought Edition 1.0.0 (#69, Aug  2 2006, 12:09:59) [MSC
 v.1310 32 bit (Intel)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import numpy
 >>> numpy.__version__
 '1.0.2'
 >>> from numpy import array
 >>> a = array([127,127], dtype='int8')
 >>> a+a
 array([-2, -2], dtype=int8)                           <== overflow (bad!)
 >>> a.sum()
 254                                                   <== correct value
 >>> a.sum().dtype
 dtype('int32')                                        <== correctly
 upgraded to larger type



 >>> b = array([1250000000,1250000000], dtype='int32')
 >>> b+b
 array([-1794967296, -1794967296])                     <== overflow (bad!)
 >>> b.sum()
 -1794967296                                           <== overflow
 >>> b.sum().dtype
 dtype('int32')                                        <== did not upgrade
 type to available int64 type
 >>>
 >>> from numpy import int64                           <== int64 exists
 >>> c = array([1250000000,1250000000], dtype='int64')
 >>> c
 array([1250000000, 1250000000], dtype=int64)
 >>> c.sum()
 2500000000                                            <== correct value
 >>> c + c
 array([2500000000, 2500000000], dtype=int64)          <== correct value
 >>>


 >>> f = array([8750000000000000000, 8750000000000000000], dtype=int64)
 >>> f+f
 array([-946744073709551616, -946744073709551616], dtype=int64)    <==
 overflow (bad too?)
 >>> f.sum()
 -946744073709551616                                               <==
 overflow (bad too?)
 >>>
 }}}


 - David Socha & Daniel Terhorst, UrbanSim Project, http://www.urbansim.org

-- 
Ticket URL: <http://projects.scipy.org/scipy/numpy/ticket/492>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.


More information about the Numpy-tickets mailing list