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:
- First exponentiation and roots
- Then multiplications and divisions
- 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:
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.
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 - 2^-3*4 = 2^(-(3*4)) ~ 0.000244140625 $\endgroup$ 2 4 Answers
$\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:
- There is no general correct rule for this kind of operation. Brackets are the way to go. There is no international standart.
- 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.
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 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$ $\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/4The 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
$\endgroup$ 5