View mode: basic / threaded / horizontal-split · Log in · Help
January 13, 2012
Re: Pow operator precedence
2012/1/13 Don Clugston <dac@nospam.com>:
> On 13/01/12 14:47, Manu wrote:
>> I would certainly have made this mistake if I tried it. And knowing this
>> information will not cause me to do it properly, it will simply make me
>> question my code, and become very suspicious every time I ever use the
>> operator (ie. I will never understand the proper precedence, I don't
>> think it makes sense).
>> I'm fairly amazed it's not the other way around... what's the logic
>> behind this?
>
>
> Originally it worked the other way, but bearophile complained about it, so
> it got changed to this way <g>.
>

I agree to this - meanwhile I've chosen to use std.math.pow to avoid confusion.

Anyway, current convention violates the consistence of the language's
rules, while other one would be confusing to people who have
python/put_language_name experience. In either case, this operator
will be ambiguous for someone. Since C doesn't have such operator, I
don't see a proper way to solve it. Maybe it would be good to always
require explicit parenthesis in such expressions?
January 13, 2012
Re: Pow operator precedence
On 1/13/2012 4:48 AM, bearophile wrote:
> This is the third time I see people trip on power operator precedence:
> http://d.puremagic.com/issues/show_bug.cgi?id=7268
>
> Some people expect this:
> (-10 ^^ 2)
> To be 100 instead of -100
> (Note: Python here uses the same operator precedences.)
>
> Do you think it's worth (and possible) to help D programmers avoid this mistake in their code?
>
> Bye,
> bearophile

I don't like it, but I think we should keep it.

The reason?

-10 ^^ 2 == -(10) ^^ 2 == -(x) ^^ 2 == -x ^^ 2 != (-x) ^^ 2
January 13, 2012
Re: Pow operator precedence
On 1/13/2012 8:59 AM, Mehrdad wrote:
> On 1/13/2012 4:48 AM, bearophile wrote:
>> This is the third time I see people trip on power operator precedence:
>> http://d.puremagic.com/issues/show_bug.cgi?id=7268
>>
>> Some people expect this:
>> (-10 ^^ 2)
>> To be 100 instead of -100
>> (Note: Python here uses the same operator precedences.)
>>
>> Do you think it's worth (and possible) to help D programmers avoid 
>> this mistake in their code?
>>
>> Bye,
>> bearophile
>
> I don't like it, but I think we should keep it.
>
> The reason?
>
> -10 ^^ 2 == -(10) ^^ 2 == -(x) ^^ 2 == -x ^^ 2 != (-x) ^^ 2
Heck, or how about emitting a warning if it's a literal without 
parentheses either way?
January 13, 2012
Re: Pow operator precedence
I feel it should be left as is: it'll be ambiguous either way and why mess
with how it's in mathematics? If anyone feels uncomfortable using it,
just use std.math.pow. Many other languages don't have this operator so
people coming from them won't know it exists anyway (like me until this
post).

— Matej

On 13 January 2012 18:01, Mehrdad <wfunction@hotmail.com> wrote:
> On 1/13/2012 8:59 AM, Mehrdad wrote:
>>
>> On 1/13/2012 4:48 AM, bearophile wrote:
>>>
>>> This is the third time I see people trip on power operator precedence:
>>> http://d.puremagic.com/issues/show_bug.cgi?id=7268
>>>
>>> Some people expect this:
>>> (-10 ^^ 2)
>>> To be 100 instead of -100
>>> (Note: Python here uses the same operator precedences.)
>>>
>>> Do you think it's worth (and possible) to help D programmers avoid this
>>> mistake in their code?
>>>
>>> Bye,
>>> bearophile
>>
>>
>> I don't like it, but I think we should keep it.
>>
>> The reason?
>>
>> -10 ^^ 2 == -(10) ^^ 2 == -(x) ^^ 2 == -x ^^ 2 != (-x) ^^ 2
>
> Heck, or how about emitting a warning if it's a literal without parentheses
> either way?
January 13, 2012
Re: Pow operator precedence
On 13 January 2012 16:09, Andrew Wiley <wiley.andrew.j@gmail.com> wrote:

> On Fri, Jan 13, 2012 at 7:47 AM, Manu <turkeyman@gmail.com> wrote:
> > On 13 January 2012 14:48, bearophile <bearophileHUGS@lycos.com> wrote:
> >>
> >> This is the third time I see people trip on power operator precedence:
> >> http://d.puremagic.com/issues/show_bug.cgi?id=7268
> >>
> >> Some people expect this:
> >> (-10 ^^ 2)
> >> To be 100 instead of -100
> >> (Note: Python here uses the same operator precedences.)
> >>
> >> Do you think it's worth (and possible) to help D programmers avoid this
> >> mistake in their code?
> >
> >
> > I would certainly have made this mistake if I tried it. And knowing this
> > information will not cause me to do it properly, it will simply make me
> > question my code, and become very suspicious every time I ever use the
> > operator (ie. I will never understand the proper precedence, I don't
> think
> > it makes sense).
> > I'm fairly amazed it's not the other way around... what's the logic
> behind
> > this?
>
> The logic is that the precedence in the language matches the
> precedence of a written equation.
>

But the operator looks nothing like the written equation... nothing at all
like the written equation.
Perhaps D could support the unicode characters '²' '³' or 'ª' as kinda
handy operators. But to me, the operator looks NOTHING like maths notation,
and it would never have occurred to me that the operator was trying to
emulate maths notation (and by extension, its precedence rules).
I'd be interested to see a poll, and how many people see it one way or the
other...
January 13, 2012
Re: Pow operator precedence
On 13 January 2012 17:56, Don Clugston <dac@nospam.com> wrote:

> On 13/01/12 14:47, Manu wrote:
>
>> On 13 January 2012 14:48, bearophile <bearophileHUGS@lycos.com
>> <mailto:bearophileHUGS@lycos.**com <bearophileHUGS@lycos.com>>> wrote:
>>
>>    This is the third time I see people trip on power operator precedence:
>>    http://d.puremagic.com/issues/**show_bug.cgi?id=7268<http://d.puremagic.com/issues/show_bug.cgi?id=7268>
>>
>>    Some people expect this:
>>    (-10 ^^ 2)
>>    To be 100 instead of -100
>>    (Note: Python here uses the same operator precedences.)
>>
>>    Do you think it's worth (and possible) to help D programmers avoid
>>    this mistake in their code?
>>
>>
>> I would certainly have made this mistake if I tried it. And knowing this
>> information will not cause me to do it properly, it will simply make me
>> question my code, and become very suspicious every time I ever use the
>> operator (ie. I will never understand the proper precedence, I don't
>> think it makes sense).
>> I'm fairly amazed it's not the other way around... what's the logic
>> behind this?
>>
>
> Originally it worked the other way, but bearophile complained about it, so
> it got changed to this way <g>.
>

Well... he should obviously be shot!
January 13, 2012
Re: Pow operator precedence
On 13 January 2012 19:41, Matej Nanut <matejnanut@gmail.com> wrote:

> I feel it should be left as is: it'll be ambiguous either way and why mess
> with how it's in mathematics? If anyone feels uncomfortable using it,
> just use std.math.pow. Many other languages don't have this operator so
> people coming from them won't know it exists anyway (like me until this
> post).


Expecting all people who may be uncomfortable with it to use pow() doesn't
help those who have to read others code containing the operator.
It's NOT like it is in mathematics, there is no 'operator' in mathematics
(maths uses a superscript, which APPEARS to be a unary operation). When
using the operator, with spaces on either side, it looks like (and is) a
binary operator.
I think it's reasonable for any experienced programmer to expect that any
binary operator will have a lower precedence than a unary operator.
What I wonder is why this operator is necessary at all? With this
ambiguity, it harms the readability, not improves it :/
January 13, 2012
Re: Pow operator precedence
On 13/01/2012 13:47, Manu wrote:
<snip>
>     Some people expect this:
>     (-10 ^^ 2)
>     To be 100 instead of -100
<snip>
> I'm fairly amazed it's not the other way around... what's the logic behind this?

It matches standard mathematical notation.  -x² means -(x²) not (-x)².

This actually makes most sense when you consider that:

(a) -2x² means -2(x²), because exponentiation beats multiplication.  With the precedence 
you're suggesting, removal of the 2 would completely change the expression.

(b) 42 - x² means 42 - (x²).  With the precedence you're suggesting, removal of the 42 
would completely change the expression.

Both these rules play a significant part in how we write polynomial expressions.  Look at 
these:

x³ - x² + 3
   - x² + 3
   -4x² + 3

In all these, the coefficient of x² is negative.  It would be confusing if it were 
positive only in the second one.

It might help to think of -Exp as syntactic sugar for 0-Exp.

Stewart.
January 13, 2012
Re: Pow operator precedence
On 13/01/2012 18:18, Manu wrote:
<snip>
> It's NOT like it is in mathematics, there is no 'operator' in mathematics (maths uses a
> superscript, which APPEARS to be a unary operation). When using the operator, with spaces
> on either side, it looks like (and is) a binary operator.
<snip>

Actually, exponentiation _is_ an operator, just like + or - or × or ÷.  Or even ∩ or ∪, 
though these have completely different domains.  Indeed, strictly speaking, there's no 
distinction between operators and functions.

What does vary between operators, however, is the structure of the _notation_.  This has 
no bearing on its intrinsic nature as an operator.  The distinction is part of 
mathematical notation, not mathematics itself.

To put it differently, you could invent a mathematical notation of your own.  Doing so 
does not intrinsically change the mathematics.

Stewart.
January 13, 2012
Re: Pow operator precedence
On 13 January 2012 20:54, Stewart Gordon <smjg_1998@yahoo.com> wrote:

> On 13/01/2012 13:47, Manu wrote:
> <snip>
>
>     Some people expect this:
>>    (-10 ^^ 2)
>>    To be 100 instead of -100
>>
> <snip>
>
>  I'm fairly amazed it's not the other way around... what's the logic
>> behind this?
>>
>
> It matches standard mathematical notation.  -x² means -(x²) not (-x)².
>
> This actually makes most sense when you consider that:
>
> (a) -2x² means -2(x²), because exponentiation beats multiplication.  With
> the precedence you're suggesting, removal of the 2 would completely change
> the expression.
>
> (b) 42 - x² means 42 - (x²).  With the precedence you're suggesting,
> removal of the 42 would completely change the expression.
>
> Both these rules play a significant part in how we write polynomial
> expressions.  Look at these:
>
> x³ - x² + 3
>   - x² + 3
>   -4x² + 3
>
> In all these, the coefficient of x² is negative.  It would be confusing if
> it were positive only in the second one.
>
> It might help to think of -Exp as syntactic sugar for 0-Exp.


I think there's one very important point to realise in all your examples
though...
We're NOT writing -4x² + 3. We write -4 * x ^^ 2 + 4. That's not a
polynomial expressions, it's source code.
I don't know about you, but the visual similarity is just not there for me.
I can't see C/D/Java/whatever code as a direct transcription of
mathematical notation no matter how hard I squint at it.
I personally have the presumption that unary operators have a higher
precedence than binary operators... period. I wouldn't give that a second
thought, and that trumps all other logic for me.
Secondary to that, when looking at that statement and deciding which of the
* or ^^ might have higher precedence, I would probably only then consider
that '^^' *may* have higher precedence than '*', but still not certain, and
I wouldn't be surprised (possibly assume) it had the same, ie. left to
right, as with * and /.
1 2 3 4
Top | Discussion index | About this forum | D home