float / double support in Python?

Alex Martelli aleax at aleax.it
Fri Feb 7 05:21:02 EST 2003


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')
>>>

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'
>>>

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


Alex





More information about the Python-list mailing list