ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   -1^2+1 vs 1-1^2 (https://www.excelbanter.com/excel-programming/342522-1%5E2-1-vs-1-1%5E2.html)

Scriptick

-1^2+1 vs 1-1^2
 
In excel =-1^2+1 returns 2 while =1-1^2 returns 0.
What calculation rule decides this? Apparently in the first -1 is
interpreted as (-1), where in the second part - is interpreted as a
substraction operator. =-(1)^2+1 also returns 2.


impslayer

-1^2+1 vs 1-1^2
 

Scriptick wrote:

In excel =-1^2+1 returns 2 while =1-1^2 returns 0.
What calculation rule decides this? Apparently in the first -1 is
interpreted as (-1), where in the second part - is interpreted as a
substraction operator. =-(1)^2+1 also returns 2.


I guess '-' is only interpreted as the unary operator if it's
first in a statement, otherwise you have to put it and its
operand inside parenthesis = (-1) for example...

/impslayer


NickHK

-1^2+1 vs 1-1^2
 
Scriptick,
Read the Help on "The order in which Microsoft Excel performs operations in
formulas":
You will see the difference between -1 and 1-1.

NickHK

"Scriptick" wrote in message
oups.com...
In excel =-1^2+1 returns 2 while =1-1^2 returns 0.
What calculation rule decides this? Apparently in the first -1 is
interpreted as (-1), where in the second part - is interpreted as a
substraction operator. =-(1)^2+1 also returns 2.




Scriptick

-1^2+1 vs 1-1^2
 
Thanks, exactly what I could not find myself.
However, when I repeat the calculations in the immediate window in the
VBE both expressions -1^2+1 and 1-1^2 evaluate to 0. This is according
to the help function ("The order in which Microsoft Excel performs
operations in formulas", but then in the VBE help), but also confusing.


impslayer

-1^2+1 vs 1-1^2
 

Scriptick skrev:

Thanks, exactly what I could not find myself.
However, when I repeat the calculations in the immediate window in the
VBE both expressions -1^2+1 and 1-1^2 evaluate to 0. This is according
to the help function ("The order in which Microsoft Excel performs
operations in formulas", but then in the VBE help), but also confusing.


Had no idea that the operator precedence order was different in the
immediate window! Guess it comes from VB where (in VB6 anyway) ^ has
higher precedence than the unary - operator. In Excel formulas it's the
other way around.

/impslayer, who should probably go quiet for a couple of years
again


NickHK

-1^2+1 vs 1-1^2
 
Scriptick,
In the VBA help of "Operator Precedence", it states that Exponentiation (^)
is higher than Negation (-); i.e. the opposite of Excel.

In "^ Operator Example";
........
MyValue = (-5) ^ 3 ' Returns -125.

Note the barckets.

So it seem you correct that Excel and VBA switch the order of precendence
for these two.

NickHK

"Scriptick" wrote in message
oups.com...
Thanks, exactly what I could not find myself.
However, when I repeat the calculations in the immediate window in the
VBE both expressions -1^2+1 and 1-1^2 evaluate to 0. This is according
to the help function ("The order in which Microsoft Excel performs
operations in formulas", but then in the VBE help), but also confusing.





All times are GMT +1. The time now is 10:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com