top | item 9050569

(no title)

peterderivaz | 11 years ago

I ran into this when computing PSNR comparisons using a Numpy function that returned a numpy integer type:

    >>> import numpy
    >>> x=0
    >>> y=numpy.int32(2*10**9)
    >>> x+=y
    >>> x+=y
    >>> x
    -294967296
In later versions of Python this gives a RuntimeWarning so this is less likely to cause problems now.

discuss

order

PhantomGremlin|11 years ago

Python 2.7.2 on OS X 10.8.5 behaves even more strangely. I get:

   >>> import numpy
   >>> x=0
   >>> y=numpy.int32(2*10**9)
   >>> y
   2000000000
   >>> x+=y
   >>> x
   2000000000
   >>> x+=y
   >>> x
   4000000000
   >>> x*x
   -2446744073709551616
   >>> y*y
   __main__:1: RuntimeWarning: overflow encountered in int_scalars
   -1651507200
I didn't think it was possible to get integer wraparound in Python. I guess it's because the "int32" from numpy "breaks" x, which subsequently no longer behaves like a python variable.