From: Charles R H. <cha...@gm...> - 2006-08-30 23:24:37
|
On 8/30/06, Lars Friedrich <lfr...@im...> 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 |