Exponentiation operator precedence
sphink at gmail.com
Thu Aug 27 18:41:15 UTC 2015
On 08/27/2015 11:20 AM, Ethan Resnick wrote:
> Long-time esdiscuss lurker; hopefully this perspective is helpful.
> I think the problem here is that traditional mathematic notation uses
> visual cues to imply precedence that JS can't take advantage of. When
> -3 ** 2 is written out on paper, the 2 is very clearly grouped
> visually with the 3. In fact, the superscript almost makes the 2 feel
> like an appendage of the 3. That makes it more natural to read it as
> two items: the negative sign, and (3 ** 2).
> By contrast, when (-3 ** 2) is written out in code, the negative sign
> is way closer visually to the 3 than the 2 is, so I find myself
> instinctively pulling out a "-3" first and reading the expression as
If we're making ** bind tighter than unary -, then I would hope it would
be written -3**2, not -3 ** 2. The latter is indeed deceptive.
For me, x**y**z is rare enough that I don't really care if ** is right
associative or nonassociative. Parentheses are part of the cost you have
to pay for rendering things as plain text -- and yet, I see no reason
not to make x**y**z just do the right thing.
More information about the es-discuss