reason icon indicating copy to clipboard operation
reason copied to clipboard

Wrong precedence for unary minus or pow

Open bsansouci opened this issue 7 years ago • 4 comments

let a = -. x ** 2.;

parses as

let a = (-. x) ** 2.;

bsansouci avatar Oct 27 '18 21:10 bsansouci

To what extend do you think that we should change this? For example in JS, this is the expected precedence: image

IwanKaramazow avatar Oct 29 '18 21:10 IwanKaramazow

@IwanKaramazow no matter how that is parsed, that will result in the same number.

I would also argue that this is not incorrect - postfix operators should bind tighter than prefix operators, which should bind tighter than any binary operators.

strega-nil avatar Oct 29 '18 21:10 strega-nil

screen shot 2018-10-30 at 9 03 20 pm

EDIT: Mmmmmmh... Honestly I think the intuition would be that the minus isn't lumped into the x^2. In math you'd write something like x^3 - x^2 - x - 1 for example. All the - aren't lumped into the expressions, the exponentiation takes precedence. So I think ideally we'd do that. If you guys disagree maybe we could make it a parse error, so that I would at least be able to fix it immediately and move on.

bsansouci avatar Oct 31 '18 04:10 bsansouci

In [1]: -2**2
Out[1]: -4

python does what you expect @bsansouci

jaredly avatar Oct 31 '18 13:10 jaredly