what is this?

Jay Dorsey jay at jaydorsey.com
Sat Nov 1 22:38:21 CET 2003


Herbert Fritsch wrote:
>>>>a = 3
>>>>b = 4
>>>>b / 2 + a
> 
> 5
> 
>>>>b / (2.0 + a)
> 
> 0.80000000000000004
> 
> There must be something wrong. I' running the new Suse 9.0.

Not really.  Think of the order of operations in mathematics. 
Multiplication and division are ranked higher than addition.  Using 
parentheses for grouping is higher (before) mulitplication and division.

First example:

4 / 2 + 3 yields 2 + 3 yields 5

Second:

4 / (2.0 + 3)

Python coerces the 3 into a floating point number, so lets write all 
these as floating points:

4.0 / (2.0 + 3.0)

Order of operations says do the parentheses first:

4.0 / 5.0 yields 0.8000000000000004

Theres a listing of Pythons order of operations somewhere in the 
documentation on python.org (couldn't find it with a quick search).

:)








More information about the Python-list mailing list