Single precision floating point calcs?

Robert Kern robert.kern at gmail.com
Wed May 9 15:51:08 EDT 2007


Grant Edwards wrote:
> I'm pretty sure the answer is "no", but before I give up on the
> idea, I thought I'd ask...
> 
> Is there any way to do single-precision floating point
> calculations in Python?   
> 
> I know the various array modules generally support arrays of
> single-precision floats.  I suppose I could turn all my
> variables into single-element arrays, but that would be way
> ugly...

We also have scalar types of varying precisions in numpy:


In [9]: from numpy import *

In [10]: float32(1.0) + float32(1e-8) == float32(1.0)
Out[10]: True

In [11]: 1.0 + 1e-8 == 1.0
Out[11]: False


If you can afford to be slow, I believe there is an ASPN Python Cookbook recipe
for simulating floating point arithmetic of any precision.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list