[Numpy-discussion] Weird clipping when astype(int) used on large numbers

Matthieu Brucher matthieu.brucher at gmail.com
Fri Oct 17 15:34:39 EDT 2008


Hi,

This a usual thing in integers conversions. If you transform an
integer like 0x0000FFFF from 16 bits to 8bits, you get 0xFFFF, thus a
negative number. As there are no processor instructions that do
saturations (DSP instructions), the behavior is to be expected.

Matthieu

2008/10/17 Tony S Yu <tonyyu at mit.edu>:
> I ran into this weird behavior with astype(int)
>
> In [57]: a = np.array(1E13)
>
> In [58]: a.astype(int)
>
> Out[58]: array(-2147483648)
>
> I understand why large numbers need to be clipped when converting to
> int (although I would have expected some sort of warning), but I'm
> puzzled by the negative value. Shouldn't the above code clip the value
> to the max positive int (2147483647)... and maybe issue a warning?
>
> Thanks,
> -Tony
>
> P.S. In case this is a problem with my install:
> OS X 10.5.5
> Python 2.5.1
> Numpy 1.2.0
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Information System Engineer, Ph.D.
Website: http://matthieu-brucher.developpez.com/
Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn: http://www.linkedin.com/in/matthieubrucher



More information about the NumPy-Discussion mailing list