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

18 messages
Open this post in threaded view
|

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

 I would expect `-2**3` to return -8, or `-2**2 == -4`, since it should be like `-(2**3)`Firefox gives a clearer error then Chrome with:> SyntaxError: unparenthesized unary expression can't appear on the left-hand side of '**'Is there a reason for this restriction? Python does it `-2**3` fine _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 probably because it's floating point native not integer like you'd think, so -2.00000001 ** 3 is hard?On Fri, Oct 14, 2016 at 4:30 AM, Cyril Auburtin wrote:I would expect `-2**3` to return -8, or `-2**2 == -4`, since it should be like `-(2**3)`Firefox gives a clearer error then Chrome with:> SyntaxError: unparenthesized unary expression can't appear on the left-hand side of '**'Is there a reason for this restriction? Python does it `-2**3` fine _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 What does it change if it handles floats?unary + is 16, while exponentiation is 15, so the opposite order that I thought2016-10-14 13:37 GMT+02:00 J Decker :probably because it's floating point native not integer like you'd think, so -2.00000001 ** 3 is hard?On Fri, Oct 14, 2016 at 4:30 AM, Cyril Auburtin wrote:I would expect `-2**3` to return -8, or `-2**2 == -4`, since it should be like `-(2**3)`Firefox gives a clearer error then Chrome with:> SyntaxError: unparenthesized unary expression can't appear on the left-hand side of '**'Is there a reason for this restriction? Python does it `-2**3` fine _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 In reply to this post by Cyril Auburtin 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 _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 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 associativeBut ok, thanks for the explanation.2016-10-14 14:05 GMT+02:00 Bergi :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 _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 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 than unaries.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 anyway2016-10-14 14:33 GMT+02:00 Cyril Auburtin :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 associativeBut ok, thanks for the explanation.2016-10-14 14:05 GMT+02:00 Bergi :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 _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 In reply to this post by Cyril Auburtin On Fri, Oct 14, 2016 at 7:31 AM Cyril Auburtin <[hidden email]> wrote:I would expect `-2**3` to return -8, or `-2**2 == -4`, since it should be like `-(2**3)`This was discussed extensively during the design process and determined that requiring user code to be explicit about its intention was the only sane design. Rick  _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 In reply to this post by Cyril Auburtin Here's some more specific notes: On Fri, Oct 14, 2016 at 7:31 AM Cyril Auburtin <[hidden email]> wrote:I would expect `-2**3` to return -8, or `-2**2 == -4`, since it should be like `-(2**3)`Math.pow(-2, 3) === -8Math.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` finePython is also inconsistent: >>> pow(-2, 2)4>>> -2 ** 2-4>>>Whereas, JavaScript is very consistent: \$ jsjs> -(2 ** 2)-4js> -Math.pow(2, 2)-4js> (-2) ** 24js> Math.pow(-2, 2)4js>Rick _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 > Le 14 oct. 2016 à 16:52, Rick Waldron <[hidden email]> a écrit : > > Python is also inconsistent: > > >>> pow(-2, 2) > 4 > >>> -2 ** 2 > -4 > >>> This is not inconsistency, but that follows from operator precedence rules (those used in mathematics, not in C). In the same vein, you have `pow(1+1, 2) == 4` but `1+1 ** 2 == 2`, because the latter is interpreted as `1+(1 ** 2)`. —Claude _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 If -2 ** 2 returned me -4 in JS I would be confused.JS is not a math language, it's a programming language. We have basic math operations on its syntax and that's fine.> In the same vein, you have `pow(1+1, 2) == 4` but `1+1 ** 2 == 2`, because the latter is interpreted as `1+(1 ** 2)`.Where is this going to? Am I supposed to read the `1+1 ** 2 == 2` is a sugar for `pow(1+1, 2)`? that's not my reading.---Python does not follow a standard implemented in many different implementations governed by different ppl and orgs. Any behaviour, even the unexpected, becomes a feature, it's an easy field and canvas. I'm glad JS aims for a consistent and unambiguous path, even if that can't make everyone loving the syntax.On Fri, Oct 14, 2016 at 11:09 AM, Claude Pache wrote: > Le 14 oct. 2016 à 16:52, Rick Waldron <[hidden email]> a écrit : > > Python is also inconsistent: > > >>> pow(-2, 2) > 4 > >>> -2 ** 2 > -4 > >>> This is not inconsistency, but that follows from operator precedence rules (those used in mathematics, not in C). In the same vein, you have `pow(1+1, 2) == 4` but `1+1 ** 2 == 2`, because the latter is interpreted as `1+(1 ** 2)`. —Claude _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 In reply to this post by Claude Pache I must say throwing here, instead of relying on math dictated operators precedence looks really bad. It's very surprising to those well experienced with the language, and  totally inconsistent with how operators worked so far (there is no previous case where one will throw for similar reason). Also argument that it's inconsistent with Math.pow(-2, 2), is total miss in my eyes. I believe to most programmers `Math.pow(-2, 2)`, translates to `(-2)**(2)` and not to `-2**2`, same as `Math.pow(a ? b : c, 2)` intuitively translates to `(a ? b : c)**(2)` and not to `a ? b : c**2`
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 It's quite simple (as has already been stated): some people expect `-x ** y` to be `-(x ** y)`. Some expect it to be `(-x) ** y`. The early SyntaxError ensures that nobody is confused - programmers will immediately add parens to disambiguate.Avoiding a potential footgun for the next 50 years, at the insignificant cost of adding two characters so that it parses seems like a very cheap price to pay.On Tue, Oct 18, 2016 at 12:20 AM, medikoo wrote:I must say throwing here, instead of relying on math dictated operators precedence looks really bad. It's very surprising to those well experienced with the language, and totally inconsistent with how operators worked so far (there is no previous case where one will throw for similar reason). Also argument that it's inconsistent with Math.pow(-2, 2), is total miss in my eyes. I believe to most programmers `Math.pow(-2, 2)`, translates to `(-2)**(2)` and not to `-2**2`, same as `Math.pow(a ? b : c, 2)` intuitively translates to `(a ? b : c)**(2)` and not to `a ? b : c**2` -- View this message in context: http://mozilla.6506.n7.nabble.com/Power-operator-why-does-2-3-throws-tp359609p359731.html Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at Nabble.com. _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 There are many other cases when with no parens involved, people have different expectations on the outcome. If expression looks ambigous the actual result always depends on operators precedence, it's how language worked for years, and I don't remember any big problems due to that. Jordan Harband wrote It's quite simple (as has already been stated): some people expect `-x ** y` to be `-(x ** y)`. Some expect it to be `(-x) ** y`. The early SyntaxError ensures that nobody is confused - programmers will immediately add parens to disambiguate. Avoiding a potential footgun for the next 50 years, at the insignificant cost of adding two characters so that it parses seems like a very cheap price to pay. On Tue, Oct 18, 2016 at 12:20 AM, medikoo <[hidden email]> wrote: > I must say throwing here, instead of relying on math dictated operators > precedence looks really bad. > It's very surprising to those well experienced with the language, and > totally inconsistent with how operators worked so far (there is no previous > case where one will throw for similar reason). > > Also argument that it's inconsistent with Math.pow(-2, 2), is total miss in > my eyes. > I believe to most programmers `Math.pow(-2, 2)`, translates to `(-2)**(2)` > and not to `-2**2`, > same as `Math.pow(a ? b : c, 2)` intuitively translates to `(a ? b : > c)**(2)` and not to `a ? b : c**2` > > > > > -- > View this message in context: http://mozilla.6506.n7.nabble. > com/Power-operator-why-does-2-3-throws-tp359609p359731.html > Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at > Nabble.com. > _______________________________________________ > es-discuss mailing list > [hidden email]> https://mail.mozilla.org/listinfo/es-discuss> _______________________________________________ es-discuss mailing list [hidden email]https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 If tc39 wanted to implement it one way or the other, they would indeed use precedence. The problem is that the precedence of unary '-' vs binary '**' is ambiguous *between different people's heads* -- not just a little, but a lot. So whichever precedence you pick, some people will be very surprised. It will be *obviously* wrong to some people, and obviously correct to others. No matter how good your mechanism is, it can't fix a policy problem. On 10/18/2016 01:05 AM, medikoo wrote: > There are many other cases when with no parens involved, people have > different expectations on the outcome. > If expression looks ambigous the actual result always depends on operators > precedence, it's how language worked for years, and I don't remember any big > problems due to that. > > > Jordan Harband wrote >> It's quite simple (as has already been stated): some people expect `-x ** >> y` to be `-(x ** y)`. Some expect it to be `(-x) ** y`. >> >> The early SyntaxError ensures that nobody is confused - programmers will >> immediately add parens to disambiguate. >> >> Avoiding a potential footgun for the next 50 years, at the insignificant >> cost of adding two characters so that it parses seems like a very cheap >> price to pay. >> >> On Tue, Oct 18, 2016 at 12:20 AM, medikoo < >> medikoo+mozilla.org@ >> > >> wrote: >> >>> I must say throwing here, instead of relying on math dictated operators >>> precedence looks really bad. >>> It's very surprising to those well experienced with the language, and >>> totally inconsistent with how operators worked so far (there is no >>> previous >>> case where one will throw for similar reason). >>> >>> Also argument that it's inconsistent with Math.pow(-2, 2), is total miss >>> in >>> my eyes. >>> I believe to most programmers `Math.pow(-2, 2)`, translates to >>> `(-2)**(2)` >>> and not to `-2**2`, >>> same as `Math.pow(a ? b : c, 2)` intuitively translates to `(a ? b : >>> c)**(2)` and not to `a ? b : c**2` >>> >>> >>> >>> >>> -- >>> View this message in context: http://mozilla.6506.n7.nabble. >>> com/Power-operator-why-does-2-3-throws-tp359609p359731.html >>> Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at >>> Nabble.com. >>> _______________________________________________ >>> es-discuss mailing list >>> >> es-discuss@ >>> https://mail.mozilla.org/listinfo/es-discuss>>> >> _______________________________________________ >> es-discuss mailing list >> es-discuss@ >> https://mail.mozilla.org/listinfo/es-discuss> > > > > -- > View this message in context: http://mozilla.6506.n7.nabble.com/Power-operator-why-does-2-3-throws-tp359609p359733.html> Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at Nabble.com. > _______________________________________________ > es-discuss mailing list > [hidden email] > https://mail.mozilla.org/listinfo/es-discuss_______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 ES is already not free from such cases, e.g. `!'property' in object` will also not resolve as most will expect. I understand and agree that case is ambigous and is problematic. It just feels very controversial to me that it was decided that this case will be handled differently than others that share exactly same problem. For those who are not aware of all such quirks/inconsistences it leaves feeling that language is unpredictable in its behaviors. On 10/18/2016 01:05 AM, medikoo wrote: > There are many other cases when with no parens involved, people have > different expectations on the outcome. > If expression looks ambigous the actual result always depends on operators > precedence, it's how language worked for years, and I don't remember any big > problems due to that. > > > Jordan Harband wrote >> It's quite simple (as has already been stated): some people expect `-x ** >> y` to be `-(x ** y)`. Some expect it to be `(-x) ** y`. >> >> The early SyntaxError ensures that nobody is confused - programmers will >> immediately add parens to disambiguate. >> >> Avoiding a potential footgun for the next 50 years, at the insignificant >> cost of adding two characters so that it parses seems like a very cheap >> price to pay. >> >> On Tue, Oct 18, 2016 at 12:20 AM, medikoo < >> medikoo+mozilla.org@ >> > >> wrote: >> >>> I must say throwing here, instead of relying on math dictated operators >>> precedence looks really bad. >>> It's very surprising to those well experienced with the language, and >>> totally inconsistent with how operators worked so far (there is no >>> previous >>> case where one will throw for similar reason). >>> >>> Also argument that it's inconsistent with Math.pow(-2, 2), is total miss >>> in >>> my eyes. >>> I believe to most programmers `Math.pow(-2, 2)`, translates to >>> `(-2)**(2)` >>> and not to `-2**2`, >>> same as `Math.pow(a ? b : c, 2)` intuitively translates to `(a ? b : >>> c)**(2)` and not to `a ? b : c**2` >>> >>> >>> >>> >>> -- >>> View this message in context: http://mozilla.6506.n7.nabble. >>> com/Power-operator-why-does-2-3-throws-tp359609p359731.html >>> Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at >>> Nabble.com. >>> _______________________________________________ >>> es-discuss mailing list >>> >> es-discuss@ >>> https://mail.mozilla.org/listinfo/es-discuss>>> >> _______________________________________________ >> es-discuss mailing list >> es-discuss@ >> https://mail.mozilla.org/listinfo/es-discuss> > > > > -- > View this message in context: http://mozilla.6506.n7.nabble.com/Power-operator-why-does-2-3-throws-tp359609p359733.html> Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at Nabble.com. > _______________________________________________ > es-discuss mailing list > [hidden email]> https://mail.mozilla.org/listinfo/es-discuss_______________________________________________ es-discuss mailing list [hidden email]https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Power operator, why does -2**3 throws?

 It's a committee-compromise grammar hack. On 3:52AM, Thu, Oct 20, 2016 medikoo <[hidden email]> wrote:ES is already not free from such cases, e.g. `!'property' in object` will also not resolve as most will expect. I understand and agree that case is ambigous and is problematic. It just feels very controversial to me that it was decided that this case will be handled differently than others that share exactly same problem. For those who are not aware of all such quirks/inconsistences it leaves feeling that language is unpredictable in its behaviors. On 10/18/2016 01:05 AM, medikoo wrote: > There are many other cases when with no parens involved, people have > different expectations on the outcome. > If expression looks ambigous the actual result always depends on operators > precedence, it's how language worked for years, and I don't remember any > big > problems due to that. > > > Jordan Harband wrote >> It's quite simple (as has already been stated): some people expect `-x ** >> y` to be `-(x ** y)`. Some expect it to be `(-x) ** y`. >> >> The early SyntaxError ensures that nobody is confused - programmers will >> immediately add parens to disambiguate. >> >> Avoiding a potential footgun for the next 50 years, at the insignificant >> cost of adding two characters so that it parses seems like a very cheap >> price to pay. >> >> On Tue, Oct 18, 2016 at 12:20 AM, medikoo < >> medikoo+mozilla.org@ >> > >> wrote: >> >>> I must say throwing here, instead of relying on math dictated operators >>> precedence looks really bad. >>> It's very surprising to those well experienced with the language, and >>> totally inconsistent with how operators worked so far (there is no >>> previous >>> case where one will throw for similar reason). >>> >>> Also argument that it's inconsistent with Math.pow(-2, 2), is total miss >>> in >>> my eyes. >>> I believe to most programmers `Math.pow(-2, 2)`, translates to >>> `(-2)**(2)` >>> and not to `-2**2`, >>> same as `Math.pow(a ? b : c, 2)` intuitively translates to `(a ? b : >>> c)**(2)` and not to `a ? b : c**2` >>> >>> >>> >>> >>> -- >>> View this message in context: http://mozilla.6506.n7.nabble. >>> com/Power-operator-why-does-2-3-throws-tp359609p359731.html >>> Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at >>> Nabble.com. >>> _______________________________________________ >>> es-discuss mailing list >>> >> es-discuss@ >>> https://mail.mozilla.org/listinfo/es-discuss >>> >> _______________________________________________ >> es-discuss mailing list >> es-discuss@ >> https://mail.mozilla.org/listinfo/es-discuss > > > > > -- > View this message in context: > http://mozilla.6506.n7.nabble.com/Power-operator-why-does-2-3-throws-tp359609p359733.html > Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at > Nabble.com. > _______________________________________________ > es-discuss mailing list > es-discuss@ > https://mail.mozilla.org/listinfo/es-discuss _______________________________________________ es-discuss mailing list es-discuss@ https://mail.mozilla.org/listinfo/es-discuss -- View this message in context: http://mozilla.6506.n7.nabble.com/Power-operator-why-does-2-3-throws-tp359609p359853.html Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at Nabble.com. _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss