Floating point (in)accuracy?

Alex Martelli aleaxit at yahoo.com
Wed Apr 25 12:59:42 EDT 2001


"Dinu Gherman" <dinu at reportlab.com> wrote in message
news:3ae6eb73.802364759 at news.vr.in-berlin.de...
> Apparently the python mailiing lists on python.org are blocked, so let
> me ask here. Is there any good reason for the following behaviour to
> be different on Python 1.5.2 and 2.0?
    [snip]
>   >>> 3.14
>   3.14
    [snip]
>   >>> 3.14
>   3.1400000000000001

Yes: repr(x) where x is a float used to HIDE some digits (trying to
provide a 'prettier' look), but that violates the mission of repr, which
is to give you as much info as it can.  Now, repr(x) "tells the truth";
use str(x) to get prettier (false:-) output.  The interpreter uses repr.

You can change that in Python 2.1, by setting sys.displayhook to
your favourite "display this result to sys.stdout" function, but that
was not in 2.0 yet.


Alex






More information about the Python-list mailing list