Power operator, why does -2**3 throws?

Rick Waldron waldron.rick at gmail.com
Fri Oct 14 14:52:08 UTC 2016


Here's some more specific notes:

On Fri, Oct 14, 2016 at 7:31 AM Cyril Auburtin <cyril.auburtin at gmail.com>
wrote:

> I would expect `-2**3` to return -8, or `-2**2 == -4`, since it should be
> like `-(2**3)`
>

Math.pow(-2, 3) === -8
Math.pow(-2, 2) === 4

To get -4: -Math.pow(-2, 2)


> Firefox gives a clearer error then Chrome with:
> > SyntaxError: unparenthesized unary expression can't appear on the
> left-hand side of '**'
>

That's correct.

>
> Is there a reason for this restriction? Python does it `-2**3` fine
>

Python is also inconsistent:

>>> pow(-2, 2)
4
>>> -2 ** 2
-4
>>>

Whereas, JavaScript is very consistent:

$ js
js> -(2 ** 2)
-4
js> -Math.pow(2, 2)
-4
js> (-2) ** 2
4
js> Math.pow(-2, 2)
4
js>



Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20161014/5c89d1ee/attachment.html>


More information about the es-discuss mailing list