Floating point (in)accuracy?

Michael Hudson mwh21 at cam.ac.uk
Wed Apr 25 15:55:39 EDT 2001


grante at visi.com (Grant Edwards) writes:

> I presume that on typical platforms, it can't be guaranteed that
> float(repr(x)) == x.

I imagine this depends on your C library's atod and printf functions
being any good.  It certainly seems to be the case here:

/>> for i in range(100000):
|..     x = random.random()
|..     if x != float(repr(x)):
|..         print x
\__ 
->> 

> If you pickle a float and then un-pickle it on the same platform are
> you guaranted to end up with the same binary representation?

Well:

->> pickle.dumps(3.14)
'F3.1400000000000001\n.'

so iff repr(float(x)) is the same as x.  But:

->> pickle.dumps(3.1,1)
'G@\x08\xcc\xcc\xcc\xcc\xcc\xcd.'

So it looks like for binary pickles the answer is "yes".

Cheers,
M.

-- 
81. In computing, turning the obvious into the useful is a living
    definition of the word "frustration".
  -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html



More information about the Python-list mailing list