[Edu-sig] Strange results

Christian Mascher christian.mascher at gmx.de
Tue Jan 3 13:07:02 CET 2012


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 at python.org
> http://mail.python.org/mailman/listinfo/edu-sig



More information about the Edu-sig mailing list