The Computer Oracle

For x=3 in Excel, why does = - x^2 + x result in 12 instead of -6?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Isolated

--

Chapters
00:00 For X=3 In Excel, Why Does = - X^2 + X Result In 12 Instead Of -6?
00:31 Accepted Answer Score 140
04:03 Answer 2 Score 20
04:17 Answer 3 Score 15
05:07 Answer 4 Score 4
05:43 Thank you

--

Full question
https://superuser.com/questions/1385570/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#microsoftexcel #worksheetfunction #notation

#avk47



ACCEPTED ANSWER

Score 140


Short answer

To solve this problem, just add a 0 before the equal sign

= 0 - A1^2 + A1

or add a couple of parentheses to force the standard order of operations

= - (A1^2) + A1

or replace the minus sign by its common interpretation of multiplication by -1

= -1 * A1^2 + A1

In this particular case, where you have the extra term +A1, the best solution is that proposed by @lioness99a:

= A1 - A1^2

Detailed explanation

Under Excel's conventions,

= - 3^2

equals (-3)^2 = 9, while

= 0-3^2

equals 0-9 = -9.

Why does adding just a 0 change the result?

Not preceded by a minuend, the minus sign in -3^2 is considered a negation operator, which is a unary operator (with only one argument) that changes the sign of the number (or parenthetical expression) that follows. However, the minus sign in 0-3^2 is a subtraction operator, which is a binary operator (with two arguments) that subtracts what follows - from what precedes -. According to Excel's conventions, the exponentiation operator ^ is computed after the negation operator and before the subtraction operator. See "Calculation operators and precedence in Excel", section "The order in which Excel performs operations in formulas".

The standard mathematical convention is that the exponentiation is computed before both negation and subtraction or, more simply stated, ^ is computed before -. Shamefully, Excel chose different conventions from those of algebra rules, school textbooks, academic writing, scientific calculators, Lotus 1-2-3, Python, mathematically oriented languages (Mathematica, Maple, Fortran, Matlab, Julia), MS Works, and... VBA (the language used to write Excel's macros). Unfortunately, Calc from LibreOffice and Google Sheets follow the same convention for compatibility with Excel. However, placing an expression in Google's search box or bar gives excellent results. The following is a discussion where a mathematician kills the arguments of a "computer scientist" defending the precedence of negation over exponentiation: Link

General Workarounds

If you want to compute

- Anything ^ 2,

add a 0 before the equal sign

0 - Anything ^ 2

or add a couple of parentheses to force the standard order of operations

- ( Anything ^ 2 )

or replace the minus sign by its common interpretation of multiplication by -1

-1 * Anything ^ 2

Of the alternatives above, I prefer adding a 0 before de minus sign because it is the most practical.

If an extra term is added (or subtracted without the even-power problem),

- Anything ^ 2 + ExtraTerm,

the best solution is to place the ExtraTerm first,

ExtraTerm - Anything ^ 2.

A comment to another answer says that the only case you have to be aware of the non-standard precedence rule is where a minus sign follows an equal sign (=-). However, there are other examples, like =exp(-x^2) or =(-2^2=2^2), where there isn't a minuend before the minus sign. Before knowing about the unexpected convention, I once wrote a Gaussian density in Excel as

= EXP(-( (x-mu)/sigma )^2 / 2 ) / sigma / SQRT( 2*PI() ),

which led to ridiculous results. I solved the problem by adding a 0 before the first - sign.

Thanks to @BruceWayne for proposing a short answer, which I wrote at the beginning.

You may be interested in According to Excel, 4^3^2 = (4^3)^2. Is this really the standard mathematical convention for the order of exponentiation?




ANSWER 2

Score 20


A bit more succint than Rodolfo's Answer, you can use:

=-(A1^2)+(A1)

(Edit: I totally didn't see it was a self question/answer.)




ANSWER 3

Score 15


The following is Excel's behavior, regardless of its convenience or reasonableness:

A leading - is considered part of the first term.

=-3^2 is processed as (-3)^2 = 9

With a zero at the start it is instead treated as normal subtraction.

=0-3^2 is processed as 0 - 3^2 = -9

And if you have two operators, then the same thing will happen.

=0--3^2 is processed as 0 - (-3)^2 = -9 and =0+-3^2 is processed as 0 + (-3)^2 = 9




ANSWER 4

Score 4


Because Excel is interpreting your equation as:

(-x)^2 + x

When you wanted:

-(x^2) + x

To prevent this sort of undesired behavior, I find the best practice is to make heavy use of parenthesis to define your own priority system, since negation is not the same as subtraction, and thus not covered by PEMDAS. An example would be like:

(-(x^2))+x

It might be overkill, but this is how I guarantee Excel behaves the way I want.