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

Bergi a.d.bergi at web.de
Fri Oct 14 12:05:48 UTC 2016


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 it?

> 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


More information about the es-discuss mailing list