![](https://secure.gravatar.com/avatar/ba366a43ea0322ddb4cf2462f8ad2596.jpg?s=120&d=mm&r=g)
Consider conversion of a slice using the astype() method:
Conversion using the astype() method goes wrong:
print b.astype(Float32) [ 1. 2.]
This fixes it:
print b.copy().astype(Float32) [ 3. 4.]
A bug? Cheers, Peter -- Dr. Peter J. Verveer Cell Biology and Cell Biophysics Programme EMBL Meyerhofstrasse 1 D-69117 Heidelberg Germany Tel. : +49 6221 387245 Fax : +49 6221 387242 Email: Peter.Verveer@embl-heidelberg.de
![](https://secure.gravatar.com/avatar/faf9400121dca9940496a7473b1d8179.jpg?s=120&d=mm&r=g)
Hi Peter, What you're demonstrating below looks like a bug in numarray which was just recently solved: https://sourceforge.net/tracker/index.php?func=detail&aid=784866&group_id=1369&atid=450446 As you've shown, numarray's astype() fails for some slices, notably those which are offset from the base of the array and therefore have a non-zero _byteoffset. This is fixed in CVS now and will be "officially released" soon. On Mon, 2003-08-18 at 08:01, Peter Verveer wrote:
![](https://secure.gravatar.com/avatar/b24e93182e89a519546baa7bafe054ed.jpg?s=120&d=mm&r=g)
# ta.py to check Int8, Int16 import numarray as _n import numarray.numerictypes as _nt b= _n.arange(4, type= _nt.Int8) print 'b, b._type:', b, b._type c= b*1001 # Grief here - type not updated print 'c, c._type:', c, c._type e= _n.array([1, -2, 3000, 4.6], type= _nt.Int8) # undocumented feature fraction discarded # and lowest eight bits retained as a signed # integer. print 'e, e._type:', e, e._type f= _n.array([1, 2, 3, 4.6], type= _nt.Int8) * 9.6 print 'f, f._type:', f, f._type g= (f.copy()*2000).astype(_nt.Int16) # undocumented - see e above print 'g, g._type:', g, g._type -------------------------------------------------------------------- PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32. Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
Colin W.
![](https://secure.gravatar.com/avatar/faf9400121dca9940496a7473b1d8179.jpg?s=120&d=mm&r=g)
These problems are (partially) addressed in numarray-0.7 with the addition of a check_overflow parameter to numarray.fromlist(). The checked fromlist is now used in the implementation of ufuncs (e.g. multiply), preventing the silent truncation of scalar values to the type of the array. Problem c: solved -- 1001 scalar was silently truncated to Int8 before multiplication. Problem e: solved -- 3000 was silently truncated to Int8 during fromlist. Use fromlist() rather than array() and add check_overflow=1 parameter. Problem f: rejected -- working as designed, won't change. Floating point numbers passed into array() are silently truncated if the type is not a floating point type. Operating on an integer array and a floating point scalar returns a floating point array by design; likewise, operating on an integer array with an integer scalar returns an integer array. This is all expected behavior. Problem g: unaddressed -- working as designed, open to discussion. The silent truncation of g to Int16 is documented in the description of astype(). It's possible to add a check_overflow flag to astype() Thanks for the feedback, Todd On Mon, 2003-08-18 at 09:20, Colin J. Williams wrote:
![](https://secure.gravatar.com/avatar/faf9400121dca9940496a7473b1d8179.jpg?s=120&d=mm&r=g)
Hi Peter, What you're demonstrating below looks like a bug in numarray which was just recently solved: https://sourceforge.net/tracker/index.php?func=detail&aid=784866&group_id=1369&atid=450446 As you've shown, numarray's astype() fails for some slices, notably those which are offset from the base of the array and therefore have a non-zero _byteoffset. This is fixed in CVS now and will be "officially released" soon. On Mon, 2003-08-18 at 08:01, Peter Verveer wrote:
![](https://secure.gravatar.com/avatar/b24e93182e89a519546baa7bafe054ed.jpg?s=120&d=mm&r=g)
# ta.py to check Int8, Int16 import numarray as _n import numarray.numerictypes as _nt b= _n.arange(4, type= _nt.Int8) print 'b, b._type:', b, b._type c= b*1001 # Grief here - type not updated print 'c, c._type:', c, c._type e= _n.array([1, -2, 3000, 4.6], type= _nt.Int8) # undocumented feature fraction discarded # and lowest eight bits retained as a signed # integer. print 'e, e._type:', e, e._type f= _n.array([1, 2, 3, 4.6], type= _nt.Int8) * 9.6 print 'f, f._type:', f, f._type g= (f.copy()*2000).astype(_nt.Int16) # undocumented - see e above print 'g, g._type:', g, g._type -------------------------------------------------------------------- PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32. Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
Colin W.
![](https://secure.gravatar.com/avatar/faf9400121dca9940496a7473b1d8179.jpg?s=120&d=mm&r=g)
These problems are (partially) addressed in numarray-0.7 with the addition of a check_overflow parameter to numarray.fromlist(). The checked fromlist is now used in the implementation of ufuncs (e.g. multiply), preventing the silent truncation of scalar values to the type of the array. Problem c: solved -- 1001 scalar was silently truncated to Int8 before multiplication. Problem e: solved -- 3000 was silently truncated to Int8 during fromlist. Use fromlist() rather than array() and add check_overflow=1 parameter. Problem f: rejected -- working as designed, won't change. Floating point numbers passed into array() are silently truncated if the type is not a floating point type. Operating on an integer array and a floating point scalar returns a floating point array by design; likewise, operating on an integer array with an integer scalar returns an integer array. This is all expected behavior. Problem g: unaddressed -- working as designed, open to discussion. The silent truncation of g to Int16 is documented in the description of astype(). It's possible to add a check_overflow flag to astype() Thanks for the feedback, Todd On Mon, 2003-08-18 at 09:20, Colin J. Williams wrote:
participants (3)
-
Colin J. Williams
-
Peter Verveer
-
Todd Miller