Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default -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.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default -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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default -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.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default -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.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default -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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default -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.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"