Hi,

apart from integer-division (see Kirby's post) the result also depends on different interpretations of the operator "-":

My friend noticed strange results from Python 2.5.2:

 >>> b=47
 >>> 0-b/2
-23

is interpreted as 0 - ( b/2) so integer division comes first before subtraction (- as subtraction operator).

 >>> -b/2+0
-24


is interpreted like ( (-1) * b ) / 2 + 0
so a leading - is applied like a _multiplication_ and because * and / are on equal levels of execution, the left-most is evaluated first.

Always interesting how seemingly simple and "straightforward" rules can trip you in special circumstances. Parentheses are recommended if you want to be sure.

Happy new year

Christian
Anybody can explain that?

Regards,
Andrzej Kapanowski


------------------------------------------------------------------------

_______________________________________________
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig

_______________________________________________
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to