float / double support in Python?

jsaul jsaul at gmx.de
Fri Feb 7 04:03:46 EST 2003


* Chad Netzer [2003-02-07 04:11]:
> On Thu, 2003-02-06 at 17:57, Isaac To wrote:
>
> > The only benefit of 4-byte floats on real computers is that it consumes less
> > space.  Computations done in 4-byte floats are seldom faster than those in
> > 8-byte doubles

Ever compared an FFT on a float vs double array?

> In graphics applications that are not fill-rate limited, or CPU limited,
> using floats for vertex (and other) data, can have a significant
> effect.  If nothing else, because sending all the data to a graphics
> renderer takes half the bandwith for a float than a double.  Also, the
> graphics chips (even expensive ones) tend to work on floats natively,
> and would otherwise convert doubles to float (the calculations may then
> occur at 'double' precision, but will usually be clipped back down to
> float in the end)
>
> Numerics, on the other hand, typically prefers to use doubles, even if
> that means sacrificing half your speed, because doubles are just so much
> more resistant to effects of badly conditioned matrices and computations
> (although doubles are FAR from capable of solving any numerical
> stability problems; they are not a panacea).  Still, float is still
> commonly used and can make a difference even on modern fast hardware
> (again, usually due to memory bandwidth issues)
>
> Fortunately, Numeric Python handles various types of floats and doubles
> natively (even if Python the interpreter thinks of a 'float' as a C
> double.)  And PyOpenGL interfaces to Numeric's native objects, so things
> are quite fast.

However, there is a serious drawback that renders Numeric arrays
problematic *unless* they are always of double type. For example

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

I would expect to be able to multiply a numeric array and a
numeric scalar independent of what either's internal type is. In
particular, I want to work with arrays of 'float' type without
actually having to worry at any point in my program what the type
currently may be (Yes, I am aware of the problems, e.g. for
"in-place" multiplication of a float array with a complex number
etc.).

The above "quiet coercion" behaviour is also extremely annoying
when I pass an array to a C extension and always have to consider
several types the array *might* have, and convert to a particular
type (computationally expensive) or provide a C function for every
type (expensive to maintain unless one uses a C++ template).

But AFAIK this and other behaviours are now being fixed in the
NumArray re-implementation.

Apart from that, Numeric is a great package, no doubt about that!

Greetings from "Old Europe", jsaul
-- 
Palo pa'que aprenda que aquí sí hay honor!
[Rubén Blades]




More information about the Python-list mailing list