[Numpy-discussion] upcast

Charles R Harris charlesr.harris at gmail.com
Wed Aug 30 18:24:34 CDT 2006


On 8/30/06, Lars Friedrich <lfriedri at imtek.de> wrote:
>
> Hello,
>
> I would like to discuss the following code:
>
> #***start***
> import numpy as N
>
> a = N.array((200), dtype = N.uint8)
> print (a * 100) / 100
>
> b = N.array((200, 200), dtype = N.uint8)
> print (b * 100) / 100
> #***stop***
>
> The first print statement will print "200" because the uint8-value is
> cast "upwards", I suppose. The second statement prints "[0 0]". I
> suppose this is due to overflows during the calculation.
>
> How can I tell numpy to do the upcast also in the second case, returning
> "[200 200]"? I am interested in the fastest solution regarding execution
> time. In my application I would like to store the result in an
> Numeric.UInt8-array.
>
> Thanks for every comment


To answer the original question, you need to use a higher precision array or
explicitly cast it to higher precision.

In [49]:(a.astype(int)*100)/100
Out[49]:array([200])

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060830/5c37845d/attachment.html 


More information about the Numpy-discussion mailing list