7
$\begingroup$

How should I evaluate 2*-2^3?

Which one of these two is the correct one?

2*((-2)^3)
2*(-(2^3))

I was wondering what was the correct operator precedence. Everyone was taught at school to evaluate expressions this way:

  1. First exponentiation and roots
  2. Then multiplications and divisions
  3. Finally additions and subtractions

However, for more complex operations, this table is incomplete. For instance, which precedence do unary operators have regarding to the others?

On the one hand, we can see that unary operators have lower precedence than the exponentiation in many cases.

-2^2 = -(2^2) = -4 (exponentiation first)

On the other hand, we sometimes use it differently (unary operators take precedence over exponentiation):

2^-2 = 2^(-2) = 1/4 = 0.25 (unary minus first)

I am a bit confused about this, so these are my questions:

  1. Which would be the general correct rule for this kind of operations? I know this ambiguous problem can easily be solved putting brackets around the unary operators (but many programming languages perform those operations without needing them). Because of this, I think there should be a rule or an international standard.

  2. How should I evaluate the following expressions?

    2*-2^3 <-- the most important
    2^-3*4
    2*-3*4
    -2^-3

In order to research how modern calculators behave, I have tested 2^-3*4 in many different calculators and each one gives me a different result:

 - Google                    2^-3*4 = (2^(-3))*4 = 0.5

 - http://web2.0calc.com/    2^-3*4 = 2^(-(3*4)) ~ 0.000244140625
$\endgroup$
2
  • $\begingroup$ Wikipedia has an article that contains the most basic order of operations. This seems to agree with most of what I was taught and it seems to agree with the C and Java precedence of operators. $\endgroup$
    – robjohn
    Dec 20, 2018 at 19:13
  • 1
    $\begingroup$ 2^(-2)… put the parentheses somewhere else. Find a placement for them that makes sense. Exactly as TonyK say, there is no other way. For -2², it's different. Although I'd say it's 4, since it's not an expession that's negated, it's a negative number (-2), and the minus sign is a sign of this number, a part of this, and not an operator. $\endgroup$ Jul 9, 2021 at 21:59

4 Answers 4

12
$\begingroup$

Not everyone was taught what you say. I was not, for example. I was never taught how to write expressions with exponents in-line, so I never found out what the canonic meaning of x^a+b actually is.

What I was taught is that whenever there is some confusion and there may exist two ways of interpreting an expression, I should use parentheses. And that is exactly what you need to start doing.

The thing is that by now, the notation has become so widely used with no central rule telling us what the only proper way of evaluation is, that it no longer makes much sense to try to impose a world-wide standard.


Taking this into consideration, the answers are:

  1. There is no general correct rule for this kind of operation. Brackets are the way to go. There is no international standart.
  2. The following expressions should be evaluated as "input unclear". If you get an expression like that to evaluate, ask the author of the expression to further explain what they meant.
$\endgroup$
10
  • $\begingroup$ thank you. I would upvote you if I could. Although I agree with you, many programming languages accept expressions like that and evaluate them as they want. I was just wondering which ones were doing it right, but I see that, actually, there is no correct answer. $\endgroup$ May 26, 2015 at 8:54
  • 2
    $\begingroup$ @BrainOverflow Basically, none of them is doing it wrong, but unless you are very used to a particular language, it's a good idea to not trust that it will evaluate expressions the way you want them to. $\endgroup$
    – 5xum
    May 26, 2015 at 8:56
  • 1
    $\begingroup$ @BrainOverflow Note that most consider 2^-2 illegal notation, thinking of unary $-$ as a shorthand for $0-$. The convention for 2^-3*4 is more in line with google, "ocalc" seems to be doing it wrong. Try to feed it 2^2*2 = 8. $\endgroup$
    – AlexR
    May 26, 2015 at 8:56
  • $\begingroup$ @5xum Most programming languages have a comprehensive section about operator precedence including all operators included in the language. Generally you can only expect that multiplication and division take precedence over addition and subtraction and that a/b*c evaluates to $a\div b\cdot c = \frac{ac}b$ $\endgroup$
    – AlexR
    May 26, 2015 at 8:59
  • 1
    $\begingroup$ @AlexR I think 2^-2 is totally legal. You can use it almost everywhere. It just means 2 raised to the power of -2, where the shown as a superscript in typical mathematical notation. Of course, someone could argue that there are implied superscripted parentheses around -2, but I would keep it simple: the fewer parentheses the less visual noise. Please see my answer. I am looking forward your feedback. $\endgroup$ Dec 20, 2018 at 21:29
7
$\begingroup$

2^-2 can only be interpreted one way, because the minus sign is next to the second argument, and the exponentiation sign isn't. It's not a matter of operator precedence.

It's only when a parameter has an operator on each side that we have to use precedence to decide. For a concrete example, FORTRAN has an exponential operator built into the language, and exponentiation has the highest precedence. Unary minus has the same precedence as binary plus and minus (with left-to-right evaluation to break ties, as e.g. -4 + 3 = (-4) + 3, not -(4+3).

$\endgroup$
9
  • $\begingroup$ I agree with you too, in the case of 2^-2 it can only be one way. However, the expression 2^-3*4 is a bit more complicated, and every one evaluates it on a different way. I have heard that FORTRAN made weird things with unary operators, so maybe it is not the best example ;) I would appreciate your help as I see that you also are a programmer. $\endgroup$ May 26, 2015 at 9:31
  • $\begingroup$ @BrainOverflow I'd say most people agree that 2^-3*4 is $2^{-3}\cdot 4 = \frac12$ and not $2^{-3\cdot 4} = 2^{-12}$. $\endgroup$
    – AlexR
    May 26, 2015 at 9:37
  • $\begingroup$ @AlexR yeah. I think so. And what about 2^-3^4? This one is giving me a terrible headache. I'd say that it equals to (2^(-3))^4. $\endgroup$ May 26, 2015 at 9:41
  • $\begingroup$ @BrainOverflow Left-to-right precedence would make this one $(2^{-3})^4 = 2^{-12}$ as you say, but especially for power towers you should always use parentheses to avoid confusion. $\endgroup$
    – AlexR
    May 26, 2015 at 10:20
  • $\begingroup$ @AlexR: Unlike the other arithmetic operators, in FORTRAN the exponentiation operator binds right-to-left, not left-to-right. This makes sense to me, because otherwise a^b^c would be just the same as a^(b*c). $\endgroup$
    – TonyK
    May 26, 2015 at 11:22
2
$\begingroup$

I know that you have accepted an answer but I am tempted to add this anyway.

The notation in your question is more typical of computing rather than mathematics. Multiplication is rarely represented by * in mathematics and exponentiation is rarely represented by ^. Your 2*-2^3 would be more likely to be written as $2 (-2)^3$. Parentheses are required less often with traditional mathematical notation. E.g. 2 ^ x + y could be written as $2^x + y$ or $2^{x + y}$ both of which are fairly unambiguous without parentheses. Similarly, $a + b / c + d$ could be written as $a + \frac{b}{c} + d$ or $\frac{a + b}{c + d}$.

However, traditional mathematical notation is not as perfect and precise as many might expect. Here are some oddities:

It is quite well agreed that $\mathbb{N}$ represents the natural numbers but less well agreed whether they include $0$.

You occasional see formulae in which $e$ is an arbitrary value (probably along with $a$, $b$ ,$c$, and $d$) but also used to represent the exponential function. Similarly $\pi$ might be the usual famous number in one place but the prime counting function in another.

$\sin^2(x)$ almost always means $(\sin(x))^2$ rather than $\sin(\sin(x))$ yet $\sin^{-1}(x)$ almost always means $\arcsin(x)$ rather than $(\sin(x))^{-1}$.

Mathematics questions will be more welcome here in traditional mathematical notation using MathJax. Computing questions are probably best in a computing group.

$\endgroup$
1
$\begingroup$

The numerical results I give below are consistent with Python, the Julia Language, Google, Bing and the calculator on my Android device. From comments by @DanielSchepler, I infer we could add Gnuplot and Maxima to the smart club.

In regular mathematical writing, when b is an exponent of a and c is an exponent of the exponent b, we have

a^b^c = a^(b^c).

Numerical example: 2^1^2 = 2^(1^2) = 2^(1) = 2.

Note that the order of computation of exponentiation operators is from right to left. [Otherwise, the answer would have been 4].

Now, let us introduce the unary negation operator -, which changes the sign of the expression that follows. It can be distinguished from the binary subtraction operator because only the latter has a minuend just before it.

The following is a compact set of only two rules to deal with exponentiation and negation (taken from the Julia Language documentation):

Rule 1: The negation operator has the same level of precedence as exponentiation.

Rule 2: The operators of this level will be solved from right to left. Algebraic examples: a^-b = a^(-b), -c^d = -(c^d), a^b^c=a^(b^c). Numerical examples: 2^-3 = 2^(-3) = 1/8, -2^3 = -(2^3) = -8, 2^1^2 = 2^(1^2) = 2.

Following the previous two rules, you can solve all the computations posed in the question plus a bonus:

2 * -2^3 = 2 * (-(2^3)) = 2 * (-8) = -16

2^-3 * 4 = (2^(-3)) * 4 = 1/(2^3) * 4 = 1/8 * 4 = 1/2

2 * -3 * 4 = 2 * (-3) * 4 = -24

-2^-3 = -(2^(-3)) = -(1 / 2^3) = -1/8

4^-1^2 = 4^(-(1^2)) = 4^(-1) = 1/4

The last example is an easy-to-compute variation of the previous. [If it were correct to compute the first member of the last expression from left to right the answer would be 4^-1^2 = (4^-1)^2 = (1/4)^2 = 1/16. However, this goes against the right-to-left mathematical convention to compute a^b^c reviewed above.]

The following is an alternative set of rules. It will be handy to list languages that fail to follow specific mathematical conventions.

Rule 1. The consecutive operators in a^-b can only be interpreted one way, because the minus sign is next to b, and the exponentiation sign isn't (@TonyK's answer). Therefore, negation is computed first and exponentiation second: a^-b = a^(-b).

Rule 2. Negation operators are obviously computed from right to left: --a = -(-a).

Rule 3. Exponentiation operators are computed from right to left: a^b^c = a^(b^c).

Rule 4. When they are not juxtaposed, exponentiation has precedence over the - operator, whether it be unary negation or binary subtraction, as the documentation of mathematically reasonable languages typically states: -a^2 = -(a^2), 0-a^2 = 0-(a^2).

Rule 3 is not followed by MS Excel, Matlab, Octave. This is odd since the venerable Fortran already followed the standard mathematical convention.

Rule 4 is not followed for unary - operator in MS Excel, Google Sheets, and some old calculators. This is really bad! It can lead to miscalculating polynomials containing an even order first term preceded by a negative sign. Before knowing this 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. Just adding parenthesis makes expressions messier. Please check elegant alternatives in https://superuser.com/questions/1385570/for-x-3-in-excel-why-does-x2-x-result-in-12-instead-of-6

$\endgroup$
5
  • $\begingroup$ I was curious so I tried it in several other tools: gnuplot: print -2**4 => -16; maxima: -2^4; => -16; octave: -2^4 => -16; bc -l: -2^4 => 16 so out of these, only bc seems to disagree on precedence of unary negation. $\endgroup$ Dec 20, 2018 at 18:26
  • $\begingroup$ @DanielSchepler Thanks for the feedback. What about 2^1^2? $\endgroup$ Dec 20, 2018 at 21:53
  • $\begingroup$ Gnuplot, Maxima and bc return 2 while Octave returns 4. $\endgroup$ Dec 20, 2018 at 21:57
  • $\begingroup$ @DanielSchepler Many thanks! Excel returns 4. Google Sheets returns 2. $\endgroup$ Dec 21, 2018 at 6:44
  • 2
    $\begingroup$ your patriotic enthusiasm for the Julia project is much appreciated! However, a language being the "latest and most sophisticated language for scientific computing, created by a team of MIT team involving mathematicians" would likely not (further) require this marketing support, would it? $\endgroup$
    – user492238
    Jan 26, 2021 at 10:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .