[Tutor] strange output

Remco Gerlich scarblac@pino.selwerd.nl
Thu, 26 Apr 2001 10:22:34 +0200


On  0, Pijus Virketis <virketis@fas.harvard.edu> wrote:
> Hi,
> 
> not that this is very significant, but perhaps someone can explain this
> curious bit of output?
> 
> Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> IDLE 0.8 -- press F1 for help
> >>> 4.2-2.5
> 1.7000000000000002 <-- ?!? Where do we get this miraculous digit?
> 
> >>># just for comparison ...
> >>> 7.0-4.0
> 3.0 <-- All's well ...
> >>> 

This has to do with the inherent inaccuracy of floating points that are
represented by a fixed number of binary digits. '4.2' has no precise
representation, neither has '1.7'.

I finally got tired of answering this question so I added an explanation to 
the FAQ, some other people should review it too since it's early morning and
I'm sure it could be worded better, but it's really time an explanation
for this is in there:
http://www.python.org/cgi-bin/faqw.py?req=show&file=faq04.098.htp

-- 
Remco Gerlich