pricision of string.atof?

Jeff Epler jepler at unpythonic.net
Tue May 7 16:37:47 EDT 2002


On Tue, May 07, 2002 at 03:59:36PM -0400, Elvis Chen wrote:
> 
> Greetings,
> 
> I'm working on some numerical analysis that requires rather good precision
> on calculation.
[...]

>>> 0.424
0.42399999999999999

If you are familiar with numerics, you'll understand that IEEE doubles
will never correctly store the value 424./1000.

This is discussed in the tutorial in an appendix, and the limitations of
standard computer "floating point" maths should be discussed in any
numerics textbook without negative value.  (the one I have nearest at
hand, ATKINS 1989, spends pages 11..41 (sections 1.1 through the end of
that chapter) talking about the issue of numerical accuracy, starting
with "1.2 Computer Representation of Numbers".  From on page 14:
    With most real numbers x, we have fl(x) != x.  Looking at the
    relative (or percentage) error, it can be shown that
	x - fl(x)
	--------- = -epsilon
	   x
    with
	-beta^(-t+1) <= epsilon <= 0	    chopped fl(x)

	-(1/2)beta^(-t+1) <= epsilon <= (1/2)beta^(-t+1)
					    rounded fl(x)

(beta is the radix of a floating-point number, typically 2 on modern
systems and AFAIR reqired in IEEE; t is the size of the mantissa in
bits, 53 for C "double"s, the Python floating point type)

There are more details, but I'll leave you to your own text.

You could use an arbitrary-precision or rational-number package, but
your performance will probably be very low.

Jeff





More information about the Python-list mailing list