[PYTHON MATRIX-SIG] Type coercion issues again - possible incompatibility in next release :-(

Jim Hugunin hugunin@mit.edu
Fri, 7 Mar 1997 16:03:47 -0500

I just fixed the recently reported bug that real_fft(a) (A function which 
expects an array of reals) would silently coerce an array of complex 
numbers by dropping their imaginary component.  This is clearly a bug and 
is now fixed.

In fixing this bug, I noticed a laxness in a number of array functions for 
coercion with loss of precision.  For example, the following code runs 
without a hitch in NumPy-beta1

>>> from Numeric import *
>>> a = arange(10, typecode=Float)
>>> a
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])
>>> asarray(a, Int)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

While this is clearly not a problem in this case, in general it's a very 
dangerous thing to automatically coerce an array of floats to ints.

With my recent modifications (strictly intended to fix the real_fft bug) 
this will now raise an exception: "TypeError: Array can not be safely cast 
to required type".

The only way to get the previous behavior is to do: "a.astype(Int)". i.e. 
an explicit coercion.

The reason that I bring this up rather than just silently fixing it is that 
I find a fair number (~5) lines in the core NumPy python code that are 
broken by this bugfix.  It's easy for me to fix them here, and in my own 
code, but I thought I should open things up for discussion before 
introducing such an incompatible change after the beta1 release.

Opinions? - Jim

MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org