[Numpy-discussion] bug triggered by array.astype()

Gerard Vermeulen gerard.vermeulen at grenoble.cnrs.fr
Sun Jan 29 01:19:02 CST 2006


On Sun, 29 Jan 2006 01:50:57 -0700
Travis Oliphant <oliphant.travis at ieee.org> wrote:

[ .. ]

> In SVN, now a Overflow error is raised instead in this example because 
> negative long integer objects are trying to be converted to unsigned 
> integers (this is a Python error being raised).  We could check for this 
> and instead do what the c-compiler would do in converting from signed to 
> unsigned objects. 
> 
> Is that preferrable?
>

Well, the current SVN does now almost what Numeric does:

>>> from Numeric import *
>>> v = arange(0, 10, 1, Float32)
>>> c = (255<<24)*cos(v)+ (255<<16)*cos(v+2*pi/3) + (255<<8)*cos(v+4*pi/3) + 255
>>> c.astype(UInt32)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OverflowError: long int too large to convert to int
>>>

However, numarray does:

>>> from numarray import *
>>> v = arange(0, 10, 1, Float32)
>>> c = (255<<24)*cos(v)+ (255<<16)*cos(v+2*pi/3) + (255<<8)*cos(v+4*pi/3) + 255
>>> c.astype(UInt32)
array([4269801984, 2294853120, 2504994432,   65861376, 1514949120,
       1225005568, 4103764992, 3209541888, 3659448448,  398681088], type=UInt32)
>>>

This is more user friendly, but may cost more CPU cycles.

Gerard




More information about the Numpy-discussion mailing list