Why does =-x^2+x for x=3 in Excel result in 12 instead of -6?

96

13

Suppose my cell A1 in an Excel spreadsheet holds the number 3. If I enter the formula

= - A1^2 + A1

in A2, then A2 shows the number 12, when it should show -6 (or -9+3)

Why is that? How can I prevent this misleading behaviour?

Rodolfo Oviedo

Posted 2018-12-18T13:52:31.647

Reputation: 1 926

19A negative number squared is a positive number. Which would make the formula 9+3. -(A1)^2 would give you -6. – Ramhound – 2018-12-18T19:08:18.210

8@Ramhound = -(A1)^2 gives 9 in Excel – Rodolfo Oviedo – 2018-12-18T19:29:30.620

68@Ramhound Powers have higher priority than minus signs in any sane environment. – Nobody – 2018-12-18T20:35:17.590

17It ought to be -(A1^2) to get -6... you need parenthesis around the operation, not just the number. Excel is just fine for math, but you need to respect order of operations, and when in doubt, use parenthesis! – SnakeDoc – 2018-12-18T21:51:27.397

2it may be worth noting that the high precedence of the unary - allows you to write e.g. = 3+-1 = 2 – A C – 2018-12-18T22:51:18.030

13This is all about order of operations and nothing to do with Excel. – YetAnotherRandomUser – 2018-12-19T13:58:29.847

2@Kevin a negative sign isn't really an operator, at least how Excel is interpreting it. If OP had written 0 - A1^2 then you get -6. Yes, negation is a thing, but it's not a standard operator. – SnakeDoc – 2018-12-19T19:14:45.160

Can you tell us why (how you obtained) "-6"? That should give you an insight into why Excel naturally gives you "12" rather. – Kris – 2018-12-20T08:41:26.913

1@Kevin No, Excel is respecting the order of operations perfectly fine. Basic math skills are required to write that kind of expression properly ;-) – Skipper – 2018-12-20T14:28:05.903

2LibreOffice Calc, Google Sheets, and Apple's Numbers are all bug-compatible with Excel in this way. – 200_success – 2018-12-22T08:46:04.590

11To all those commenters criticizing the OPs math skills: In standard pure math, this should unambiguously evaluate to -6. In particular, the negative is understood to always mean 0-x (as indicated in the answer). The introduction of a unary - is a topic that is new in applied computer science, and almost always just an implementation detail. Criticizing the OP for not understanding this is like answering a question about confusion with regards to floating point errors with "the program is fine. You just need to check your math. See, if you write these numbers in binary then..." – DreamConspiracy – 2018-12-22T12:21:26.063

1I disagree with @AC about 3+-1. There's no ambiguity there, so precedence resolution isn't necessary. – Ben Millwood – 2018-12-26T15:09:53.403

1@YetAnotherRandomUser: I agree that this is about "order of operations", but it has to do with Excel's order of operations rules. Excel could have been designed with a different set of order of operations rules (actually, I expect that this goes back to decisions made when Lotus 1-2-3 was being designed (Excel was designed to mimic Lotus)) – Flydog57 – 2018-12-26T21:36:35.773

Answers

136

Short answer

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

= 0 - A1^2 + A1

or add a couple of parenthesis 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 adding just a 0 changes 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 expression) that follows. However, the minus sign in 0-3^2 is a subtraction operator, which is a binary operator 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, Mathematica, Maple, computations oriented languages like Fortran or Matlab, 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 give excellent results. If you press enter, the order of computations will be given by using parentheses. A discussion where a mathematician kills the arguments of a "computer scientist" defending the precedence of negation over exponenciation: http://mathforum.org/library/drmath/view/69058.html

General Workarounds

If you want to compute

- Anything ^ 2,

add a 0 before the equal sign

0 - Anything ^ 2

or add a couple of parenthesis 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 the expression is already surrounded by parentheses, I avoid adding parentheses. Heavy use of parentheses makes expressions harder to read, debug and write.

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 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.

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?

Rodolfo Oviedo

Posted 2018-12-18T13:52:31.647

Reputation: 1 926

1

Comments are not for extended discussion; this conversation has been moved to chat.

– DavidPostill – 2018-12-21T19:13:05.530

Please see above. All comments will be deleted if they are not part of the chat discussion. – DavidPostill – 2018-12-21T19:32:06.100

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.)

BruceWayne

Posted 2018-12-18T13:52:31.647

Reputation: 2 508

1Exactly! Depending on any language or application's precedence rules to be what you think they ought to be is a recipe for trouble. – jamesqf – 2018-12-18T17:58:21.987

2@jamesqf, but there must be some sense and limits to this. Nobody writes 2+(3*4). If a language has arithmetic operations and any precedence rules at all, it absolutely must support all the standard mathematical conventions. There is no excuse for such blunder in Excel. – Zeus – 2018-12-18T23:39:06.547

4@Zeus: Nobody? I probably would, especially if it was in a more complicated expression, or an if-condition. Of course I would write 3 *4 + 2 even if I were leaving out the parens. – jamesqf – 2018-12-19T00:57:15.660

3I long had a suspicion than such a habit of parens overuse comes from the (oh so prevalent) over-exposure to C (and its syntactic descendants). But C is by no means a good example of correct following of the math rules, including precedence (plus it has issues with macros). By contrast, people with initial exposure to more academic systems/languages strongly expect correct design and don't tend to make forward concessions 'just in case'. Hence genuine surprises like in the OP. – Zeus – 2018-12-19T02:31:56.863

14

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

trapper

Posted 2018-12-18T13:52:31.647

Reputation: 248

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.

routhken

Posted 2018-12-18T13:52:31.647

Reputation: 41

4“since PEMDAS is not guaranteed in Excel” — No, it is absolutely guaranteed in Excel. Anything else would be madness. Whether or not unary negation (which is distinct from subtraction!) has precedence over exponentiation isn’t covered by PEMDAS. – Konrad Rudolph – 2018-12-19T17:02:54.783

1@routhken Heavy use of parentheses makes writing, editing and debugging cumbersome. To ease those tasks, I check the precedence of operators in the software I am working with and use only the necessary parentheses. In addition, I add spaces to improve readability. – Rodolfo Oviedo – 2018-12-20T11:56:12.280

@KonradRudolph I edited my answer, thank you for the clarification. – routhken – 2018-12-21T03:06:06.703

1I'd prefer using x - x^2. This ensures the - is interpreted as the binary subtraction operator. – Xalorous – 2018-12-21T13:00:08.747

@KonradRudolph I think the way to look at this is that spreadsheets and computer programming languages use PUEMDAS where unary operations are evaluated after parenthetical operations but before binary mathematical operations. – Xalorous – 2018-12-21T13:02:42.057

@Xalorous This is the best solution. It had been proposed by lioness99a in a formal answer nearly at the same time as your comment. Please upvote the QUESTION if you liked it. – Rodolfo Oviedo – 2019-07-05T15:02:15.603

3

You can have it either way:

=-A1^2+A1

will return a 12, but:

=0-A1^2+A1

will return a -6

If you feel that returning 12 violates common sense; be aware that Google Sheets does the same thing.

Gary's Student

Posted 2018-12-18T13:52:31.647

Reputation: 15 540

1It seems like the unary minus sign has "too high" precedence. – Andreas Rejbrand – 2018-12-18T19:04:59.060

@AndreasRejbrand It appears to be unitary only if it directly follows the = sign............... =A1-A1^2 also returns -6 – Gary's Student – 2018-12-18T19:09:25.337

2But in the example A1 - A1^2, the minus sign is obviously binary. (A unary operator is one that takes a single operand (like the unary minus sign in -5, or the factorial, the not sign etc.); a binary operator is one that takes two operands (like binary plus, minus, multiplication, union, etc.).) Notice that the minus sign can be unary even if it doesn't follow immediately after the equals sign: 5 + (-4 + 3). – Andreas Rejbrand – 2018-12-18T19:15:13.827

@AndreasRejbrand I agree with you completely! – Gary's Student – 2018-12-18T19:16:43.233

Just to defend Google's reputation, try the search box or bar to input mathematical expressions. You will get very results consistent to good mathematics, even better that from Matlab or Octave, for example, try 2^1^2. – Rodolfo Oviedo – 2018-12-21T01:29:45.880

Your point about google sheets doing that is not particularly relevant; they most likely implemented it to match excel so that excel sheets imported into google sheets will be more likely to work correctly, not because it's the best way of doing things. – alex.forencich – 2018-12-27T23:50:42.493

@Gary'sStudent The minus sign is interpreted as unary operator wherever there is no minuend before the minus sign. Example: =exp(-x^2). Your alternative, =A1-A1^2, is the best. Please upvote the QUESTION if you liked it. – Rodolfo Oviedo – 2019-07-05T15:25:48.973

3

The expression = - A1^2 + A1 is specific to Excel so must follow Excels rules. Contrary to some other answers here, there is no correct order of precedence. There are merely different conventions adopted by different applications. For your reference, the order of precedence used by excel is:

:       Range
<space> intersection
,       union
-       Negation
%       Percentage
^       Exponential
* and / Multiplication and Division
+ and - Addition and Subtraction
&       Concatenation
= < > <= >= <>  Comparison

Which you can override using parentheses.

Paul Smith

Posted 2018-12-18T13:52:31.647

Reputation: 147

9Of course, Excel could have chosen + to mean multiplication and * to mean subtraction etc. and anyone needing to use Excel would have to know it. But it would have been wrong. The case in point is not the same level of wrong (or silly), but you definitely can argue that Excel defined got the priorities wrong. – Mormegil – 2018-12-20T07:37:59.713

4@Mormegil Well said! Once you try =1+2*2 and see that the answer is 5 and not 6. you are led to assume that Excel follows the rules of algebra. What is the point of misleading people? – Rodolfo Oviedo – 2018-12-20T07:56:56.190

There IS a correct order of precedence, but computers have additional operations. The problem here is that computers use '-' for negation AND for subtraction where the person doing written algebra sees discriminates between negation and subtraction. For the computer to tell the difference it needs a set of rules. In '-x', the '-' is a unary operator (acts on one operand). In '1-x', the '-' is a binary operator. So, Excel (and other computer software) converts -x^2 to (-x)^2. The rest of the order of precedence still applies as we all learned it in grade school. – Xalorous – 2018-12-21T13:10:43.760

3@Xalorous: Yes, - can be unary or binary. But that doesn't imply an order of operations. Other languages get this right: in Python, Ruby, Octave, Awk, and Haskell (the first five languages with an exponentiation operator that came to mind), -3 ** 2 always evaluates to -9. Why? Because that is the correct answer. – wchargin – 2018-12-22T10:27:34.720

@Xalorous What distinction between negation and subtraction does a person see in written algebra? They are visually the same (though the spacing differs conventionally). – Konrad Rudolph – 2018-12-22T12:03:19.420

1@Xalorous the person doing written algebra uses the conventions of their audience combined with parentheses to reduce ambiguity. There is no correct order of precedence, and the rules of algebra are actually just conventions. – Paul Smith – 2018-12-23T13:47:54.893

3

Alternatively, you could just do

= A1 - A1^2

because -y + x = x-y

lioness99a

Posted 2018-12-18T13:52:31.647

Reputation: 131

This doesn't explain why that should work, and duplicates numerous earlier answers. – fixer1234 – 2018-12-23T21:16:28.533

@fixer1234 Literally no one else has said that, and I have given the mathematical reason for it? – lioness99a – 2018-12-23T23:33:28.860

>

  • Many answers describe turning this into an explicit subtraction. 2. That's not the mathematical reason. The question is about why Excel does not behave that way. The answer is that the negative is not treated as subtraction by Excel.
  • < – fixer1234 – 2018-12-24T20:04:11.627

    They asked how to prevent the behaviour. I showed them the simplest way. And not a single answer states what I have... – lioness99a – 2018-12-24T20:29:34.877

    The best solution. I added to my answer with due credit. If you liked the QUESTION, please upvote it. – Rodolfo Oviedo – 2019-07-05T14:32:05.303

    2

    Other people have answered the "how can I avoid this?" part of the question. I am going to tell you why it happens.

    It happens because personal computers in 1979 had very limited memory and processing capability.

    VisiCalc was introduced for the Apple II in 1979, two years before the initial release of the IBM PC (to which most modern desktop and laptop computers trace their direct ancestry). The Apple II could be had with up to 64 KiB (65,536 bytes) of RAM, and VisiCalc required at least 32 KiB to run. As a bit of an aside here, VisiCalc is rather widely considered to be the "killer application" for the Apple II, and perhaps indeed for personal microcomputers in general.

    The fewer special cases and less formula look-ahead is required, the simpler (and by consequence smaller) the code to parse a spreadsheet formula can be made. It would therefore make sense to require the user to be somewhat more explicit in corner cases, in exchange for being able to handle larger spreadsheets. Remember, even with a high-end Apple II, you only had a few tens of kilobytes to play with after the memory required by the application was accounted for. With a low-memory system (48 KiB RAM wasn't an uncommon configuration for a "serious" machine), the limit was even lower.

    When IBM introduced their PC, a port of VisiCalc to the new architecture was made. Wikipedia refers to this port as "bug compatible", so you'd very much expect to see the exact same formula parsing behavior, even if the system technically was capable of more complex parsing.

    Beginning in 1982, Microsoft competed with VisiCalc, and later 1-2-3, with their Multiplan cross-platform spreadsheet. Later on, Lotus 1-2-3 was introduced in 1983 specifically for the IBM PC, and quickly overtook VisiCalc on it. To make the transition easier, it made sense for both to parse formulas in the same way that VisiCalc did. So the limited look-ahead behavior would be carried forward.

    In 1985, Microsoft introduced Excel, originally for the Macintosh and beginning with version 2 in 1987 to the PC. Again, to make the transition easier, it made sense to carry forward the formula parsing behavior that people were already used to since by now almost a decade.

    With each upgrade of Excel, the opportunity to change the behavior existed, but not only would it require users to learn a new way to type formulas, it would also risk breaking compatibility with spreadsheets used or created with the previous version. In a still very competitive market with several commercial companies competing with each other in each field, the decision was likely made to keep the behavior users were accustomed to.

    Fast forward to 2019, and we're still stuck with the formula parsing behavior decisions originally made no later than 1978-1979.

    a CVn

    Posted 2018-12-18T13:52:31.647

    Reputation: 26 553

    http://mathforum.org/library/drmath/view/69058.html and http://macnauchtan.com/pub/precedence.html#_Aworks report that Lotus 1-2-3 follows common algebraic conventions. – Rodolfo Oviedo – 2019-05-10T15:27:40.373

    0

    The expression - A1^2 contains two operators, namely the unary negation operator - and the binary exponentiation operator ^. With the absence of any parenthesis, there could be two interpretations. Either:

    -(A1^2)
    

    or:

    (-A1)^2
    

    The first one says first do the exponentiation with operands A1 and 2, and then do the negation on that.

    The second one says first do the negation on operand A1, and then use exponentiation on the result of that and 2.

    As was said in the comments to the question, Powers have higher priority than minus signs in any sane environment. Which means, it is best if a system assumes the first one.

    However, Excel prefers the second one.

    The lesson is, if you are unsure whether your environments is sane or not, include the parenthesis to be on the safe side. So write -(A1^2).

    Jeppe Stig Nielsen

    Posted 2018-12-18T13:52:31.647

    Reputation: 369

    This duplicates the accepted answer and other earlier answers. – fixer1234 – 2018-12-23T21:19:31.297

    -1

    This is not a problem with excel but with exponents and negatives. When you take a number and raise it to an even power, you cancel the negative sign.

    -x^2 + x == (-x * -x) + x 
    x = 3  => (-3 * -3) + 3
           ==  9 + 3 => 12
    

    You need to use parenthesis and multiple by -1

    -1 * (x^2) + x
    

    nitrodmr

    Posted 2018-12-18T13:52:31.647

    Reputation: 11

    10This is not how signs work. It should be: x = 3 => -(3 * 3) + 3 = 6. Excel just doesn't use standard algebraic convention. – henning -- reinstate Monica – 2018-12-19T15:01:00.203

    3@henning As mentioned in another comment, while this isn’t the “standard” convention, it’s a convention, albeit not the most common one. To say that “this is not how signs work” is therefore quite incorrect. Rather, this isn’t how signs work in prevailing usage. – Konrad Rudolph – 2018-12-19T17:01:19.747

    2@KonradRudolph Fair enough. I admit, I'm only aware of the prevailing usage, which, I believe, is what caused OP to be confused. – henning -- reinstate Monica – 2018-12-19T17:05:26.317

    6No, it absolutely is a problem with Excel. Excel uses the wrong rules for operator precedence. – Dawood says reinstate Monica – 2018-12-19T18:40:05.413

    Please note the difference between -x^2 where x is 3 and x^2 where x is -3. -x^2+x will never reach 12: https://www.wolframalpha.com/input/?i=-x%5E2%2Bx

    – Thomas Weller – 2018-12-20T07:49:59.230

    -2

    -x^2+x where x =3 This is an example of a quadratic equation The equation can be written like this: -3*-3+3 :Multiplication takes precedence over addition so result will be written as follows: 9 + 3 :Why =9 because a negative number x a negative number gives a positive result. This can be verified using any calculator, slide rule, or any computer mathematics program Final result 9 + 3 = 12

    Cripple2Cripple

    Posted 2018-12-18T13:52:31.647

    Reputation: 1

    -3

    It is just a really simple maths.

    Rule 1. Even multiplications of negative numbers, would output a positive result:

    minus * minus = plus

    minus * minus * minus = minus

    minus * minus * minus * minus = plus

    This is due to the fact, that minuses cancel each other in pairs.

    Rule 2. The power of every number identifies that this number will be multiplied by itself a number of times.

    (2)^n, where n=2 => 2*2 = 4

    (-2)^n, where n=2 => (-2)*(-2) = 4

    And if you can see Rule number 1..

    (-3)^n, where n=3 => (-3) * (-3) * (-3) = 9 * (-3) = -27

    Rule 3. Multiplication and Division have higher priority, than addition and subtraction.

    3*5+2 = 15+2 = 17

    3*(5+2) = 3*7 = 21

    And there is the answer of your question:

    Combining all 3 rules from before:

    -x^2 + x, where x=3 => -3^2+3 = 9+3 = 12

    My advice to you is to spend some time every year and keep refreshing the fundamental rules of mathematics.

    It is in fact a skill you can maintain and stay on top of a large portion of the world, only by knowing basic maths.

    Michael John

    Posted 2018-12-18T13:52:31.647

    Reputation: 3

    9When you write "Combining all 3 rules from before: -x^2 + x, where x=3 => -3^2+3 = 9+3 = 12" you are assuming that -x^2 = (-x)^2. You have not stated that assumption before. Therefore your conclusion is unwarranted. Actually, if you read mathematics textbook or Wikipedia, you will notice that your implied assumption is not followed. Mathematics textbooks, scientific papers, etc. follow the assumption that -x^2 = -(x^2) – Rodolfo Oviedo – 2018-12-20T12:31:30.013

    rule 3 applies to binary operators like +-*/, but not unary operators like - or +. Precedence of the power operator is higher than * and / but unary operators have even higher precedence – phuclv – 2018-12-23T03:45:25.143

    In reply to @RodolfoOviedo, you are not correct. There is a huge difference between -(x)^2 and -x^2. And it is quite clear. There is no point of me stating that out. No disrespect sir, but I feel that you just tried to comeback with a passive aggressive manner. I am sorry if I've said something that insults you. I am only here to help. – Michael John – 2018-12-29T17:43:48.147