float / double support in Python?

jsaul jsaul at gmx.de
Fri Feb 7 12:40:10 CET 2003


* Alex Martelli [2003-02-07 11:21]:
> jsaul wrote:
>    ...
> > However, there is a serious drawback that renders Numeric arrays
> > problematic *unless* they are always of double type. For example
>
> ...*OR* you know a bit more of Numeric's basics...
>
> >>>> from Numeric import *
> >>>> a=arange(0,10,'f')
> >>>> a
> > array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.],'f')
> >>>> a.typecode()
> > 'f'
> >>>> a*=1.
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > TypeError: return array has incorrect type
> >>>> a=a*1.
> >>>> a.typecode()
> > 'd'
>
> What you can do...:
>
> >>> a=arange(0, 10, typecode='f')
> >>> a.savespace(1)
> >>> a*=1.
> >>> a
> array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.],'f')
> >>>

Oh yes, that's good news. The bad news is, unfortunately:
>>> a.typecode()
'f'
>>> b = a*2j
>>> b.typecode()
'f'
>>> b
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],'f')

I would have "hoped" to get a type 'F' (complex float) array here,
but of course that would involve a coercion and therefore be
against the definition of savespace. It would however be good to
have something like ".savebasetype()" or so. As it is now, the
user is likely to run into unexpected (and hard to debug)
behaviour sooner or later, particularly because some code might
have set the savespace flag quietly. But having .savespace() is
certainly better than having no control over coercions at all.

Fortunately these issues are addressed in NumArray.

> I'm not sure how you manage to call arange like that -- must
> be some older version of Numeric?  the one I happen to have
> at hand right now has:
> >>> Numeric.__version__
> '21.0b1'

Mine is '22.0'

> But the point is the .savespace method.  It lets you work
> in-place and keep the typecode fixed.

Thanks!

Cheers, jsaul
-- 
Palo pa'que aprenda que aquí sí hay honor!
[Rubén Blades]




More information about the Python-list mailing list