Power operator, why does -2**3 throws?
cyril.auburtin at gmail.com
Fri Oct 14 12:59:49 UTC 2016
I was testing something https://caub.github.io/misc/calculator, and I
didn't see how it would be a problem to have the precedence of ** higher
But at least I'm happy something like (2).pow(3) wasn't chosen.
Thanks anyway and sorry for discussing something already frozen in spec
2016-10-14 14:33 GMT+02:00 Cyril Auburtin <cyril.auburtin at gmail.com>:
> Ah, ok, a bit sad because all more scientific languages, and python too,
> all math books, all will use `-e^3` for meaning `-(e^3)` (^ or **), because
> it's just `-exp(3)` or `-pow(E, 3)`
> and `(-1)^n` otherwise, when we want to take the signs with.
> If you wanted to avoid any confusion you could have forbidden `2**2**3`
> too because it's not obvious it's right associative
> But ok, thanks for the explanation.
> 2016-10-14 14:05 GMT+02:00 Bergi <a.d.bergi at web.de>:
>> Cyril Auburtin schrieb:
>>> I would expect `-2**3` to return -8, or `-2**2 == -4`, since it should be
>>> like `-(2**3)`
>> You would, others would not. -2 ** 2 clearly should return 4, shouldn't
>> Is there a reason for this restriction? Python does it `-2**3` fine
>> Because of the ambiguity it has been decided to make it a syntax error if
>> the two operators are used together. If you want `-(2**3)`, you have to
>> write it like that, and if you want `(-2)**3` you have to write it
>> explicitly as well.
>> See https://esdiscuss.org/topic/exponentiation-operator-precedence for
>> the full discussion.
>> - Bergi
>> es-discuss mailing list
>> es-discuss at mozilla.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss