[Numeric] why is Float32 incorrect for ufuncs?

Russell E. Owen no at spam.invalid
Thu Mar 4 21:42:42 CET 2004

In article <404757b9$1 at maser.urz.unibas.ch>,
 Curzio Basso <curzio.basso at unibas.ch> wrote:

>Hello everyone,
>I am a beginner with Numerical Python, and there is a thing I do not 
>understand in the behaviour of the ufuncs. Maybe someone can enlighten me.
>Why is the following not working?
> >>> import Numeric
> >>> a=Numeric.ones((2,2),Numeric.Float32)
> >>> a
>array([[ 1.,  1.],
>        [ 1.,  1.]],'f')
> >>> a/=2
>Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>TypeError: return array has incorrect type
>If I use Numeric.Float64 as typecode everything works fine, but I do not 
>understand why is this needed.

As another poster noted, the division is probably casting the result up 
to python float type (which, if true, corresponds to Float64), and the 
error comes from trying to reassign that result to the original array.

I suggest you try numarray (the planned replacement for Numeric; it is 
similar enough that you will probably not have to change your code and 
it handles casting better.

>>> import numarray
>>> a = numarray.ones((2,2), numarray.Float32)
>>> a
array([[ 1.,  1.],
       [ 1.,  1.]], type=Float32)
>>> a/= 2
>>> a
array([[ 0.5,  0.5],
       [ 0.5,  0.5]], type=Float32)

-- Russell

More information about the Python-list mailing list