[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-ideas
Subject:    Re: [Python-ideas] division oddness
From:       Brett Cannon <brett () python ! org>
Date:       2010-05-07 18:46:39
Message-ID: t2lbbaeab101005071146g4cd9a6e8p7782cc0ecc4f6df2 () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


On Thu, May 6, 2010 at 17:43, Mathias Panzenböck <
grosser.meister.morti@gmx.net> wrote:

> Shouldn't by by mathematical definition -x // y be the same as -(x // y)?
> But when assign x=2, y=3 you get:
>
> >>> -2 // 3
> -1
> >>> -(2 // 3)
> 0
>
> And also:
> >>> 2 // -3
> -1
> >>> -2 // -3
> 0
>
> And even more:
> >>> int(-2 / 3)
> 0
> >>> int(2 / -3)
> 0
>
> I think this rather odd. Is there any deeper reason to this behaviour? I
> guess changing this will break a lot of code, but why does it behave like
> this?
>

Operator precedence; unary negation (the 'factor' rule from Grammar/Grammar)
binds more tightly than // (the 'term' rule), thus applying the negation to
'2' before applying the '//'. Making it bind as `-(x // y)` would require
introducing a special grammar rule just to make sure that unary negation on
binary operators like this worked this way. And I am not even sure if that
would be unambiguous in Python's LL(1) grammar.

-Brett


>
>        -panzi
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>

[Attachment #5 (text/html)]

<br><br><div class="gmail_quote">On Thu, May 6, 2010 at 17:43, Mathias Panzenböck \
<span dir="ltr">&lt;<a \
href="mailto:grosser.meister.morti@gmx.net">grosser.meister.morti@gmx.net</a>&gt;</span> \
wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px \
#ccc solid;padding-left:1ex;">

Shouldn&#39;t by by mathematical definition -x // y be the same as -(x // y)? But \
when assign x=2, y=3 you get:<br> <br>
&gt;&gt;&gt; -2 // 3<br>
-1<br>
&gt;&gt;&gt; -(2 // 3)<br>
0<br>
<br>
And also:<br>
&gt;&gt;&gt; 2 // -3<br>
-1<br>
&gt;&gt;&gt; -2 // -3<br>
0<br>
<br>
And even more:<br>
&gt;&gt;&gt; int(-2 / 3)<br>
0<br>
&gt;&gt;&gt; int(2 / -3)<br>
0<br>
<br>
I think this rather odd. Is there any deeper reason to this behaviour? I guess \
changing this will break a lot of code, but why does it behave like \
this?<br></blockquote><div><br></div><div>Operator precedence; unary negation (the \
&#39;factor&#39; rule from Grammar/Grammar) binds more tightly than // (the \
&#39;term&#39; rule), thus applying the negation to &#39;2&#39; before applying the \
&#39;//&#39;. Making it bind as `-(x // y)` would require introducing a special \
grammar rule just to make sure that unary negation on binary operators like this \
worked this way. And I am not even sure if that would be unambiguous in Python&#39;s \
LL(1) grammar.</div>

<div><br></div><div>-Brett</div><div>  </div><blockquote class="gmail_quote" \
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <br>
            -panzi<div><div></div><div class="h5"><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" \
target="_blank">Python-ideas@python.org</a><br> <a \
href="http://mail.python.org/mailman/listinfo/python-ideas" \
target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br> \
</div></div></blockquote></div><br>



_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
http://mail.python.org/mailman/listinfo/python-ideas


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic