Bizarre floating-point output

Richard Brodie R.Brodie at rl.ac.uk
Mon Jan 8 09:06:36 EST 2007


"Nick Maclaren" <nmm1 at cus.cam.ac.uk> wrote in message 
news:enthjb$p0l$1 at gemini.csx.cam.ac.uk...
>
> x = (1.234567890125, 1.2345678901255)
> print x
> print x[0], x[1]
>
>>>> (1.2345678901249999, 1.2345678901254999)
>>>> 1.23456789012 1.23456789013
>
> Is there a rational reason, or is that simply an artifact of the way
> that the code has evolved?  It is clearly not a bug :-)

print x[0] gives the same result as printing str(x[0]),
the value of x formatted as a string (rounded to a
sensible number of places).

x[0] at the command prompt gives the same result as
printing repr(x), the representation of the text value as
a string.

When you do print on a tuple it doesn't recursively
call str(), so you get the repr representations.

You can get similar results with anything where the
str() and repr() values are different.
e.g. x = ( u'a', u'b') 





More information about the Python-list mailing list