[Tutor] Hummm...dubious behavior.
Tim Peters
tim.one@home.com
Sat, 15 Dec 2001 15:34:06 -0500
[Jean Montambeault]
> Felt ready for Guido's idea of a tutorial, tryed the examples, tryed
> some variations, can't help it and here's something curious :
>
> >>> tax=17.5/100
> >>> price=3.50
> >>> price*tax
> 0.61249999999999993
> >>> price+_
> 4.1124999999999998
> >>> round(_,2)
> 4.1100000000000003 # in this case : trailing zeros and an error
> >>> ui=3.5555
> >>> price*ui
> 12.44425
> >>> round(_,2)
> 12.44 # here the expectec behavior
> >>> tax*ui
> 0.62221249999999995
> >>> round(_,2)
> 0.62 # here just as well
>
> Obviously my question is what caused the difference in behavior,
> when must I expect it or best how to avoid it since, really, I can't
> imagine a use for the "long" version.
This kind of stuff is explained in a new (for Python 2.2) Tutorial appendix.
You can read it now here:
http://python.sourceforge.net/devel-docs/tut/node14.html
Python 2.2 final should be released next Friday.
This example in the tutorial has also been changed, but in a devious way, to
use numbers that happen to display in a prettier way:
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
>>>