Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: -2 ** 2

40 views
Skip to first unread message

Jon Ribbens

unread,
May 4, 2017, 10:16:43 AM5/4/17
to
On 2017-05-04, Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> In a recent Firefox:
>
> -2 ** 2
>
> SyntaxError: unparenthesized unary expression can't appear on
> the left-hand side of '**'
>
> . Can't seem to find the corresponding rule in a recent copy
> of ECMAScript (12.6), though.

Chrome behaves similarly:

-2 ** 2
Uncaught SyntaxError: Unexpected token **
(-2) ** 2
4

Presumably this is a sensible thing to avoid bugs, e.g. Python does
this:

>>> -2**2
-4
>>> (-2)**2
4

I think most people would expect both expressions to equal 4.

Robert Heller

unread,
May 4, 2017, 12:01:15 PM5/4/17
to
There is uncertainity as what the presidence of unary - vs binary **/^ should
be -- different languages do different things. One way out is to require
parens.

>

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

Sam E

unread,
May 4, 2017, 1:04:17 PM5/4/17
to
On 05/04/2017 09:13 AM, Jon Ribbens wrote:

[snip]

> >>> -2**2
> -4
> >>> (-2)**2
> 4
>
> I think most people would expect both expressions to equal 4.
>

Unless parentheses are used, exponentiation is supposed to be performed
before subtraction. -4 is correct for the first expression.

BTW, you might want to try to find the square root or -4. Both of them
(EVERY number has 2 square roots).


Mark Lloyd

unread,
May 4, 2017, 1:08:57 PM5/4/17
to
On 05/04/2017 10:56 AM, Tim Streater wrote:
> In article <slrnogmdu6.5...@sable.unequivocal.eu>, Jon
> Ribbens <jon+u...@unequivocal.eu> wrote:
>


[snip]

>> >>> -2**2
>> -4
>> >>> (-2)**2
>> 4
>>
>> I think most people would expect both expressions to equal 4.
>
> Depends on operator precedence.
>

I was always taught "Especially My Dear Aunt Sally" to remember the
order of precedence, although I never needed it (understanding works
better). Exponentiation has higher precedence that subtraction.

What actually happens depends of whether '-' is recognized as
subtraction or the unary operator (I'd expect subtraction). Anyway, to
avoid problems I's use parentheses.

--
Mark Lloyd
http://notstupid.us/

"SENILE.COM found. Out Of Memory."

John G Harris

unread,
May 4, 2017, 2:29:10 PM5/4/17
to
On Thu, 4 May 2017 12:04:12 -0500, Sam E
<why.sho...@be.email.invalid> wrote:

<snip>
>(EVERY number has 2 square roots).

0 doesn't :-)

John

John G Harris

unread,
May 4, 2017, 3:02:23 PM5/4/17
to
On Thu, 04 May 2017 16:56:06 +0100, Tim Streater
<timst...@greenbee.net> wrote:

>In article <slrnogmdu6.5...@sable.unequivocal.eu>, Jon
>Ribbens <jon+u...@unequivocal.eu> wrote:
>
>>On 2017-05-04, Stefan Ram <r...@zedat.fu-berlin.de> wrote:
>>> In a recent Firefox:
>>>
>>> -2 ** 2
>>>
>>> SyntaxError: unparenthesized unary expression can't appear on
>>> the left-hand side of '**'
>>>
>>> . Can't seem to find the corresponding rule in a recent copy
>>> of ECMAScript (12.6), though.
<snip>

See sections 12.4, 12.5, 12.6 of the Standard. You can't have
-x ** y
because a unary minus can't act on an exponentiation expression.


>Depends on operator precedence.

Like some similar languages, ECMAScript doesn't specify operator
precedence; it's the production rules in the syntax specification that
decide how an expression is to be interpreted.

John

Jon Ribbens

unread,
May 4, 2017, 7:48:03 PM5/4/17
to
On 2017-05-04, Tim Streater <timst...@greenbee.net> wrote:
> In article <slrnogmdu6.5...@sable.unequivocal.eu>, Jon
> Ribbens <jon+u...@unequivocal.eu> wrote:
>>Presumably this is a sensible thing to avoid bugs, e.g. Python does
>>this:
>>
>> >>> -2**2
>> -4
>> >>> (-2)**2
>> 4
>>
>>I think most people would expect both expressions to equal 4.
>
> Depends on operator precedence.

You don't say. It's almost as if that was the very subject of my post
that you are replying to.

Thomas 'PointedEars' Lahn

unread,
May 5, 2017, 2:17:21 AM5/5/17
to
Stefan Ram wrote:

> In a recent Firefox:
>
> -2 ** 2
>
> SyntaxError: unparenthesized unary expression can't appear on
> the left-hand side of '**'
>
> . Can't seem to find the corresponding rule in a recent copy
> of ECMAScript (12.6), though.

Next time, *cite* your sources, OK?

<http://www.ecma-international.org/ecma-262/7.0/#sec-exp-operator>

| 12.6 Exponentiation Operator
|
| Syntax
|
| ExponentiationExpression[Yield] :
| UnaryExpression[?Yield]
| UpdateExpression[?Yield] ** ExponentiationExpression[?Yield]

(via /Statement/ → /ExpressionStatement/ → /Expression/ →
/AssignmentExpression/ → … → /MultiplicativeExpression/)

*The longest match wins*, so check the second (longer) alternative first:
“-2” must be producible by UpdateExpression. Check by looking at the
possible productions for that:

| 12.4 Update Expressions
|
| Syntax
|
| UpdateExpression :
| LeftHandSideExpression[?Yield]
| LeftHandSideExpression[?Yield][no LineTerminator here]++
| LeftHandSideExpression[?Yield][no LineTerminator here]--
| ++ UnaryExpression[?Yield]
| -- UnaryExpression[?Yield]

Conclusion: “-2” cannot be produced by the /UpdateExpression/ production’s
results.

The only remaining alternative is /UnaryExpression/:

| 12.5 Unary Operators
|
| Syntax
|
| UnaryExpression :
| UpdateExpression[?Yield]
| delete UnaryExpression
| void UnaryExpression
| typeof UnaryExpression
| + UnaryExpression
| - UnaryExpression
| ~ UnaryExpression
| ! UnaryExpression

And therein lies the problem.

“-2” cannot be produced by /UpdateExpression/, so it must be producible by
“- UnaryExpression” for “-2 ** 2” to be syntactically valid.

But “2” cannot be produced by /UnaryExpression/, because it can neither be
produced by /UpdateExpression/ nor any other /UnaryExpression/ production
result.

Conclusion: “-2 ** 2” is *syntactically invalid*.

[This has nothing to do with operator precedence.]

Finding out why “(-2) ** 2” is valid syntax, and works as intended, is left
as an exercise to the reader.

--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Evertjan.

unread,
May 5, 2017, 3:16:18 AM5/5/17
to
Jon Ribbens <jon+u...@unequivocal.eu> wrote on 05 May 2017 in
comp.lang.javascript:
True.

Even so, it seems that the way the minus sign is parsed,
the unary behavour is unnoticed in this case
by the parsing engine [back to js, please]:

let r1 = -2 ^ 2; // -2

... even if it is just unforgivable as in:

let r2 = 0 + -2 ^ 2; // -2

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

John G Harris

unread,
May 5, 2017, 4:41:20 AM5/5/17
to
On Thu, 04 May 2017 19:51:33 +0100, Tim Streater
<timst...@greenbee.net> wrote:

>In article <vksmgcp498jf6afkp...@4ax.com>, John G Harris
>What about on 1s-complement computers?

I was talking maths. Computers can be approximate, or plain wrong.

John

John G Harris

unread,
May 5, 2017, 4:56:46 AM5/5/17
to
On Fri, 05 May 2017 09:16:13 +0200, "Evertjan."
<exxjxw.h...@inter.nl.net> wrote:

<snip>
>Even so, it seems that the way the minus sign is parsed,
>the unary behavour is unnoticed in this case
>by the parsing engine [back to js, please]:
>
>let r1 = -2 ^ 2; // -2
>
>... even if it is just unforgivable as in:
>
>let r2 = 0 + -2 ^ 2; // -2

The problem is that
-3**2
could be parsed as
(-3)**2 == 9
or as
-(3**2) == -9

Which would you prefer? How would you justify your choice to people
complaining loudly that the other way is the *obvious* way!

I'm guessing the ECMA262 committee decided to avoid arguments.

John

Evertjan.

unread,
May 5, 2017, 5:16:53 AM5/5/17
to
John G Harris <ni...@jghnorth.org.uk.invalid> wrote on 05 May 2017 in
comp.lang.javascript:

> On Fri, 05 May 2017 09:16:13 +0200, "Evertjan."
> <exxjxw.h...@inter.nl.net> wrote:
>
> <snip>
>>Even so, it seems that the way the minus sign is parsed,
>>the unary behavour is unnoticed in this case
>>by the parsing engine [back to js, please]:
>>
>>let r1 = -2 ^ 2; // -2
>>
>>... even if it is just unforgivable as in:
>>
>>let r2 = 0 + -2 ^ 2; // -2
>
> The problem is that
> -3**2
> could be parsed as
> (-3)**2 == 9
> or as
> -(3**2) == -9
>
> Which would you prefer? How would you justify your choice to people
> complaining loudly that the other way is the *obvious* way!

I said what I find unforgivable parsing above,
as it is clearly an unary minus,
I repeat it here:

let r2 = 0 + -2 ^ 2; // -2;

I'm not in the least interested in justifying choices on demand.

> I'm guessing the ECMA262 committee decided to avoid arguments.

If that were the case they obviously failed.

Methinks your guess is wrong, as the ECMA262 committee should go for
symplicity, logic and consistency, not for avoiding arguments.

After all, what is wrong with arguments, unless you are indoctrinated from
childhood that that is against some deity's will?

Tim Slattery

unread,
May 5, 2017, 11:03:39 AM5/5/17
to
Tim Streater <timst...@greenbee.net> wrote:

>"That excessive bail ought not to be required, nor excessive fines imposed,
>nor cruel and unusual punishments inflicted" -- Bill of Rights 1689

1689? The Bill of Rights became part of the constitution on December
15, 1791.

--
Tim Slattery
tim <at> risingdove <dot> com

Thomas 'PointedEars' Lahn

unread,
May 5, 2017, 11:47:40 AM5/5/17
to
Tim Slattery wrote:

> Tim Streater <timst...@greenbee.net> wrote:
>> "That excessive bail ought not to be required, nor excessive fines
>> imposed, nor cruel and unusual punishments inflicted" -- Bill of Rights
>> 1689
>
> 1689? The Bill of Rights became part of the constitution on December
> 15, 1791.

There is not only the United States of America.

Mark Lloyd

unread,
May 6, 2017, 12:20:53 AM5/6/17
to
On 05/05/2017 10:02 AM, Tim Slattery wrote:
> Tim Streater <timst...@greenbee.net> wrote:
>
>> "That excessive bail ought not to be required, nor excessive fines imposed,
>> nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
>
> 1689? The Bill of Rights became part of the constitution on December
> 15, 1791.
>

https://en.wikipedia.org/wiki/Bill_of_Rights_1689

--
Mark Lloyd
http://notstupid.us/

"Why doesn't God behave in such a way as to be worthy of worship?"
[Barry O'Grady, ba...@it.com.au]

John G Harris

unread,
May 6, 2017, 5:27:51 AM5/6/17
to
On Fri, 05 May 2017 11:16:13 +0200, "Evertjan."
<exxjxw.h...@inter.nl.net> wrote:

<snip>
>I said what I find unforgivable parsing above,
>as it is clearly an unary minus,
>I repeat it here:
>
>let r2 = 0 + -2 ^ 2; // -2;

Equally clearly a negative number is being raised to the power 2, so
the result is +4, not -4.


>I'm not in the least interested in justifying choices on demand.
<snip>

But you are willing to say your choice is the only right one.

John

Evertjan.

unread,
May 6, 2017, 5:42:30 AM5/6/17
to
John G Harris <ni...@jghnorth.org.uk.invalid> wrote on 06 May 2017 in
comp.lang.javascript:

> On Fri, 05 May 2017 11:16:13 +0200, "Evertjan."
> <exxjxw.h...@inter.nl.net> wrote:
>
> <snip>
>>I said what I find unforgivable parsing above,
>>as it is clearly an unary minus,
>>I repeat it here:
>>
>>let r2 = 0 + -2 ^ 2; // -2;
>
> Equally clearly a negative number is being raised to the power 2, so
> the result is +4, not -4.

Sorry, meant -4 and 4.

>>I'm not in the least interested in justifying choices on demand.
> <snip>
>
> But you are willing to say your choice is the only right one.

NOT AT ALL.

[I don't under stand the "willing to" part, I either say something or I
don't in a NG. Or did you forget a question-mark?]

I do not claim that I am right, I try to learn from discussion,
and, starting off with my argumented ideas, I am open to argumented
arguments to the contrary.

"the only right one"?

I do not think there is a single right way of defining operator precedence
in JS-parsing, but we still can argue about [personal or group] preference.

Tim Slattery

unread,
May 6, 2017, 11:21:43 AM5/6/17
to
Tim Streater <timst...@greenbee.net> wrote:

>In article <vq4pgch7qi0t27p9i...@4ax.com>, Tim Slattery
><t...@risingdove.com> wrote:
>
>>Tim Streater <timst...@greenbee.net> wrote:
>>
>>>"That excessive bail ought not to be required, nor excessive fines imposed,
>>>nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
>>
>>1689? The Bill of Rights became part of the constitution on December
>>15, 1791.
>
>Look up the 1689 Bill of Rights in Winky, you fathead.

A British bill of Rights? OK, I didn't know that. The language you
quoted is almost identical to the 8th amendment to the US
Constitution:

Excessive bail shall not be required, nor excessive fines imposed, nor
cruel and unusual punishments inflicted.

No doubt whoever drafted our 8th (George Mason, I thin?) cribbed from
the older document.

Thomas 'PointedEars' Lahn

unread,
May 6, 2017, 1:04:49 PM5/6/17
to
Tim Slattery wrote:

> Tim Streater <timst...@greenbee.net> wrote:
>> In article <vq4pgch7qi0t27p9i...@4ax.com>, Tim Slattery
>> <t...@risingdove.com> wrote:
>>> Tim Streater <timst...@greenbee.net> wrote:
>>>> "That excessive bail ought not to be required, nor excessive fines
>>>> imposed, nor cruel and unusual punishments inflicted" -- Bill of
>>>> Rights 1689
>>> 1689? The Bill of Rights became part of the constitution on December
>>> 15, 1791.
>> Look up the 1689 Bill of Rights in Winky, you fathead.
>
> A British bill of Rights? OK, I didn't know that. The language you
> quoted is almost identical to the 8th amendment to the US
> Constitution:
> […]

That might have to do with the fact that the United States of America
*originated from British colonies*…

<https://en.wikipedia.org/wiki/United_States#Settlements> pp.

(Good grief. I am not a citizen of the USA and know more about its history
than you. No surprise that DeVos managed to become Secretary of Education…)

> No doubt whoever drafted our 8th (George Mason, I thin?)

_James Madison_ (later 4th POTUS)

> cribbed from the older document.

<https://en.wikipedia.org/wiki/United_States_Bill_of_Rights#Crafting_amendments>
pp.

Tim Slattery

unread,
May 7, 2017, 12:40:22 PM5/7/17
to
Thomas 'PointedEars' Lahn <Point...@web.de> wrote:


>That might have to do with the fact that the United States of America
>*originated from British colonies*…

Of course

>(Good grief. I am not a citizen of the USA and know more about its history
>than you.

Don't bet on it.

No surprise that DeVos managed to become Secretary of Education…)
>
>> No doubt whoever drafted our 8th (George Mason, I thin?)
>
>_James Madison_ (later 4th POTUS)

He was strongly influence by Mason, who was a delegate to the
Constitutional Convention, but refused to sign the document because it
had no declaration of rights. Madison's product had much to do with
Mason's Virginia Declaration of Rights.

Thomas 'PointedEars' Lahn

unread,
May 7, 2017, 2:36:03 PM5/7/17
to
Tim Slattery wrote:

> Thomas 'PointedEars' Lahn <Point...@web.de> wrote:
>>> No doubt whoever drafted our 8th (George Mason, I thin?)
>>_James Madison_ (later 4th POTUS)
>
> He was strongly influence by Mason,

That is correct, in a sense:

> who was a delegate to the Constitutional Convention, but refused to sign
> the document because it had no declaration of rights. Madison's product
> had much to do with Mason's Virginia Declaration of Rights.

Mason drafted the Virginia Declaration of Rights based on the English Bill
of Rights, and Madison was influenced by that. However, Madison also knew
well the English Bill of Rights, among other historical documents of state,
and you asked who drafted the *amendmends*; that was Madison, not Mason.
(See also the reference I gave.)

--
PointedEars

Dr J R Stockton

unread,
May 8, 2017, 6:47:19 PM5/8/17
to
In comp.lang.javascript message <vksmgcp498jf6afkpqavb926g2rhaoj6uh@4ax.
com>, Thu, 4 May 2017 19:29:07, John G Harris
<ni...@jghnorth.org.uk.invalid> posted:
Javascript has two zeroes. One is positive and the other is negative.
They are equal in value; but it seems that (in Firefox & Chrome in WinXP
on PC:) they have different square roots of equal value but different
sign - at least, the following expression returns false :-
1/Math.sqrt(-0) == 1/Math.sqrt(+0)
That might represent a bug in ECMA 262 or its implementation.


Math.sqrt is not the only thing in JavaScript which can return -0 ; some
while ago now, it became evident here that The Great Panjandrum Himself
was inadequately aware of that, when the ability was at least useful and
perhaps almost indispensable.

--
(c) John Stockton, Surrey, UK. 拯merlyn.demon.co.uk Turnpike v6.05 MIME.
Merlyn Web Site < > - FAQish topics, acronyms, & links.


Dr J R Stockton

unread,
May 8, 2017, 6:49:58 PM5/8/17
to
In comp.lang.javascript message <vq4pgch7qi0t27p9io6u7569mkv0u5fh6l@4ax.
com>, Fri, 5 May 2017 11:02:38, Tim Slattery <t...@risingdove.com>
posted:

>Tim Streater <timst...@greenbee.net> wrote:
>
>>"That excessive bail ought not to be required, nor excessive fines imposed,
>>nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
>
>1689? The Bill of Rights became part of the constitution on December
>15, 1791.

1791 was a mere re-statement or amendment of 1689, which was inherited
in, IIRC, 1776 unless stated otherwise.

Michael Haufe (TNO)

unread,
May 9, 2017, 12:55:09 PM5/9/17
to
Dr J R Stockton wrote:
> Javascript has two zeroes. One is positive and the other is negative.
> They are equal in value; but it seems that (in Firefox & Chrome in WinXP
> on PC:) they have different square roots of equal value but different
> sign - at least, the following expression returns false :-
> 1/Math.sqrt(-0) == 1/Math.sqrt(+0)
> That might represent a bug in ECMA 262 or its implementation.
>
> Math.sqrt is not the only thing in JavaScript which can return -0 ; some
> while ago now, it became evident here that The Great Panjandrum Himself
> was inadequately aware of that, when the ability was at least useful and
> perhaps almost indispensable.
>

See <http://2ality.com/2012/03/signedzero.html>

Dr J R Stockton

unread,
May 10, 2017, 6:44:53 PM5/10/17
to
In comp.lang.javascript message <sqrt-2017...@ram.dialup.fu-
berlin.de>, Tue, 9 May 2017 06:06:45, Stefan Ram <r...@zedat.fu-
berlin.de> posted:

>Dr J R Stockton <repl...@merlyn.demon.co.uk.invalid> writes:
>>sign - at least, the following expression returns false :-
>>1/Math.sqrt(-0) == 1/Math.sqrt(+0)
>
> Such an expression can also return ›false‹ when both
> values are equal (i.e., NaN). But you just can look
> directly at ›Math.sqrt( -0 )‹ in some debuggers:
>
>Math.sqrt( -0 )
>-0
>
> . (However,
>
>String( Math.sqrt( -0 ))
>"0"
>
> .) ›Math.sqrt‹ surely is specified so in ECMAScript,
> and, according to Wikipedia, ›sqrt‹ is specified this
> way in IEEE 754.


This is a slightly longer demonstration :

alert( 1/Math.sqrt(+0) + " differs greatly from " + 1/Math.sqrt(-0) )

IIRC, the sign of any arbitrary non-NaN Number X, including the zeroes
and the infinities, can be determined by evaluating
Z = X + 1/X
and comparing Z, never smaller than 2, with being greater than or less
than zero.

A JavaScript NaN must possess a sign bit; but the value of that bit is
not discoverable in JavaScript.


--
(c) John Stockton, Surrey, UK. ¬@merlyn.demon.co.uk Turnpike v6.05 MIME.

RobG

unread,
May 10, 2017, 8:47:02 PM5/10/17
to

Thomas 'PointedEars' Lahn

unread,
May 10, 2017, 9:00:45 PM5/10/17
to
(Are you debating that the United States started out as British colonies
[which was my point]? *Really*?)

*Read* that. Read the references in what I referred to, and read

<https://en.wikipedia.org/wiki/Reliability_of_Wikipedia> (and note the
references therein)

and

<https://en.wikipedia.org/wiki/Wikipedia:Verifiability>

F'up2 poster

--
PointedEars

Twitter: @PointedEars2

Thomas 'PointedEars' Lahn

unread,
May 10, 2017, 9:08:30 PM5/10/17
to
Stefan Ram wrote:

> This program also shows that JavaScript can be used to
> /script Java/.

1. No, it does not. You are using Java to execute JavaScript
code, which is the opposite of that.

2. Tell news.

<https://en.wikipedia.org/wiki/NPAPI#LiveConnect> (pp.)

> That must be why it's called »JavaScript«!

Nonsense. Read, e.g., my thesis.
0 new messages