Why does unary minus operator sometimes take precedence over exponentiation, and sometimes not?
$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:
- 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
- http://web2.0calc.com/ 2^-3*4 = 2^(-(3*4)) ~ 0.000244140625
soft-question notation
$endgroup$
add a comment |
$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:
- 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
- http://web2.0calc.com/ 2^-3*4 = 2^(-(3*4)) ~ 0.000244140625
soft-question notation
$endgroup$
$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 '18 at 19:13
add a comment |
$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:
- 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
- http://web2.0calc.com/ 2^-3*4 = 2^(-(3*4)) ~ 0.000244140625
soft-question notation
$endgroup$
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
- http://web2.0calc.com/ 2^-3*4 = 2^(-(3*4)) ~ 0.000244140625
soft-question notation
soft-question notation
edited Jul 17 '18 at 17:37
trincot
1054
1054
asked May 26 '15 at 8:33
BrainOverflowBrainOverflow
1306
1306
$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 '18 at 19:13
add a comment |
$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 '18 at 19:13
$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 '18 at 19:13
$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 '18 at 19:13
add a comment |
4 Answers
4
active
oldest
votes
$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.
$endgroup$
$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$
– BrainOverflow
May 26 '15 at 8:54
1
$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 '15 at 8:56
1
$begingroup$
@BrainOverflow Note that most consider2^-2
illegal notation, thinking of unary $-$ as a shorthand for $0-$. The convention for2^-3*4
is more in line with google, "ocalc" seems to be doing it wrong. Try to feed it2^2*2 = 8
.
$endgroup$
– AlexR
May 26 '15 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 thata/b*c
evaluates to $adiv bcdot c = frac{ac}b$
$endgroup$
– AlexR
May 26 '15 at 8:59
$begingroup$
@AlexR Yes, of course, one method is to learn the operator precedence of each language. But I think it's better to add some unnecesary brackets, since that means that more people can read your code.
$endgroup$
– 5xum
May 26 '15 at 9:01
|
show 5 more comments
$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$
$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$
– BrainOverflow
May 26 '15 at 9:31
$begingroup$
@BrainOverflow I'd say most people agree that2^-3*4
is $2^{-3}cdot 4 = frac12$ and not $2^{-3cdot 4} = 2^{-12}$.
$endgroup$
– AlexR
May 26 '15 at 9:37
$begingroup$
@AlexR yeah. I think so. And what about2^-3^4
? This one is giving me a terrible headache. I'd say that it equals to(2^(-3))^4
.
$endgroup$
– BrainOverflow
May 26 '15 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 '15 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 '15 at 11:22
|
show 4 more comments
$begingroup$
The numerical results I give below are consistent with Julia language, the latest and most sophisticated language for scientific computing, created by a team of MIT team involving mathematicians. All computations are also consistent with Google. Good for Google! When there are more than one equal signs, i.e, more than three members, in an expression the computations in Julia and Google are made with the first member, which states the problem, so I am not cheating.
In regular mathematical writing,
a^b^c = a^(b^c). [a^b^c in not equal to (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 negation operator, i.e., the unary -
operator that changes the sign of the expression that follows. It can be distinguished from the binary subtraction operator because the negation operator does not have a minuend just to the left.
Rule 1: The negation operator has the same level of precedence as exponentiation.
Rule 2: At this level, the operators of Rule 1 will be solved from right to left. Examples: a^-b = a^(-b) and -c^d = -(c^d). Numerical examples: 2^-3 = 2^(-3) = 1/8 and -2^3 = -(2^3) = -8.
Following the previous two rules, you can solve all the required computations:
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
2^-3^4 = 2^(-(3^4)) = 2^(-81) = 1 / 2^81 = a positive number very close to 0
4^-1^2 = 4^(-(1^2)) = 4^(-1) = 1/4
The second last example was posed in a comment. 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.]
As an alternative with equal results to Rules 1 and 2, one can state the following rules:
- Exponentiation has precedence over negation, as the documentation of mathematically reasonable languages typically state.
- Exponentiation operators are computed from right to left.
- Negation operators are computed from right to left.
- (following @TonyK's answer) The consecutive operators in
a^-b
can only be interpreted one way, because the minus sign is next tob
, and the exponentiation sign isn't. Therefore, operators precedence need not be considered.
Thanks to @BrainOverflow for telling us about Google. It uses the right mathematical conventions unlike Matlab and Excel for a^b^c, and unlike Excel for -a^(2*n), where n is an integer.
$endgroup$
$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, onlybc
seems to disagree on precedence of unary negation.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 18:26
$begingroup$
@DanielSchepler Thanks for the feedback. What about 2^1^2?
$endgroup$
– Rodolfo Oviedo
Dec 20 '18 at 21:53
$begingroup$
Gnuplot, Maxima and bc return 2 while Octave returns 4.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 21:57
$begingroup$
@DanielSchepler Many thanks! Excel returns 4. Google Sheets returns 2.
$endgroup$
– Rodolfo Oviedo
Dec 21 '18 at 6:44
add a comment |
$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$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f1299236%2fwhy-does-unary-minus-operator-sometimes-take-precedence-over-exponentiation-and%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$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.
$endgroup$
$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$
– BrainOverflow
May 26 '15 at 8:54
1
$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 '15 at 8:56
1
$begingroup$
@BrainOverflow Note that most consider2^-2
illegal notation, thinking of unary $-$ as a shorthand for $0-$. The convention for2^-3*4
is more in line with google, "ocalc" seems to be doing it wrong. Try to feed it2^2*2 = 8
.
$endgroup$
– AlexR
May 26 '15 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 thata/b*c
evaluates to $adiv bcdot c = frac{ac}b$
$endgroup$
– AlexR
May 26 '15 at 8:59
$begingroup$
@AlexR Yes, of course, one method is to learn the operator precedence of each language. But I think it's better to add some unnecesary brackets, since that means that more people can read your code.
$endgroup$
– 5xum
May 26 '15 at 9:01
|
show 5 more comments
$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.
$endgroup$
$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$
– BrainOverflow
May 26 '15 at 8:54
1
$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 '15 at 8:56
1
$begingroup$
@BrainOverflow Note that most consider2^-2
illegal notation, thinking of unary $-$ as a shorthand for $0-$. The convention for2^-3*4
is more in line with google, "ocalc" seems to be doing it wrong. Try to feed it2^2*2 = 8
.
$endgroup$
– AlexR
May 26 '15 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 thata/b*c
evaluates to $adiv bcdot c = frac{ac}b$
$endgroup$
– AlexR
May 26 '15 at 8:59
$begingroup$
@AlexR Yes, of course, one method is to learn the operator precedence of each language. But I think it's better to add some unnecesary brackets, since that means that more people can read your code.
$endgroup$
– 5xum
May 26 '15 at 9:01
|
show 5 more comments
$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.
$endgroup$
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.
answered May 26 '15 at 8:38
5xum5xum
90.6k394161
90.6k394161
$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$
– BrainOverflow
May 26 '15 at 8:54
1
$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 '15 at 8:56
1
$begingroup$
@BrainOverflow Note that most consider2^-2
illegal notation, thinking of unary $-$ as a shorthand for $0-$. The convention for2^-3*4
is more in line with google, "ocalc" seems to be doing it wrong. Try to feed it2^2*2 = 8
.
$endgroup$
– AlexR
May 26 '15 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 thata/b*c
evaluates to $adiv bcdot c = frac{ac}b$
$endgroup$
– AlexR
May 26 '15 at 8:59
$begingroup$
@AlexR Yes, of course, one method is to learn the operator precedence of each language. But I think it's better to add some unnecesary brackets, since that means that more people can read your code.
$endgroup$
– 5xum
May 26 '15 at 9:01
|
show 5 more comments
$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$
– BrainOverflow
May 26 '15 at 8:54
1
$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 '15 at 8:56
1
$begingroup$
@BrainOverflow Note that most consider2^-2
illegal notation, thinking of unary $-$ as a shorthand for $0-$. The convention for2^-3*4
is more in line with google, "ocalc" seems to be doing it wrong. Try to feed it2^2*2 = 8
.
$endgroup$
– AlexR
May 26 '15 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 thata/b*c
evaluates to $adiv bcdot c = frac{ac}b$
$endgroup$
– AlexR
May 26 '15 at 8:59
$begingroup$
@AlexR Yes, of course, one method is to learn the operator precedence of each language. But I think it's better to add some unnecesary brackets, since that means that more people can read your code.
$endgroup$
– 5xum
May 26 '15 at 9:01
$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$
– BrainOverflow
May 26 '15 at 8:54
$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$
– BrainOverflow
May 26 '15 at 8:54
1
1
$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 '15 at 8:56
$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 '15 at 8:56
1
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 '15 at 8:56
$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 '15 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 $adiv bcdot c = frac{ac}b$$endgroup$
– AlexR
May 26 '15 at 8:59
$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 $adiv bcdot c = frac{ac}b$$endgroup$
– AlexR
May 26 '15 at 8:59
$begingroup$
@AlexR Yes, of course, one method is to learn the operator precedence of each language. But I think it's better to add some unnecesary brackets, since that means that more people can read your code.
$endgroup$
– 5xum
May 26 '15 at 9:01
$begingroup$
@AlexR Yes, of course, one method is to learn the operator precedence of each language. But I think it's better to add some unnecesary brackets, since that means that more people can read your code.
$endgroup$
– 5xum
May 26 '15 at 9:01
|
show 5 more comments
$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$
$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$
– BrainOverflow
May 26 '15 at 9:31
$begingroup$
@BrainOverflow I'd say most people agree that2^-3*4
is $2^{-3}cdot 4 = frac12$ and not $2^{-3cdot 4} = 2^{-12}$.
$endgroup$
– AlexR
May 26 '15 at 9:37
$begingroup$
@AlexR yeah. I think so. And what about2^-3^4
? This one is giving me a terrible headache. I'd say that it equals to(2^(-3))^4
.
$endgroup$
– BrainOverflow
May 26 '15 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 '15 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 '15 at 11:22
|
show 4 more comments
$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$
$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$
– BrainOverflow
May 26 '15 at 9:31
$begingroup$
@BrainOverflow I'd say most people agree that2^-3*4
is $2^{-3}cdot 4 = frac12$ and not $2^{-3cdot 4} = 2^{-12}$.
$endgroup$
– AlexR
May 26 '15 at 9:37
$begingroup$
@AlexR yeah. I think so. And what about2^-3^4
? This one is giving me a terrible headache. I'd say that it equals to(2^(-3))^4
.
$endgroup$
– BrainOverflow
May 26 '15 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 '15 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 '15 at 11:22
|
show 4 more comments
$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$
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).
answered May 26 '15 at 9:17
TonyKTonyK
42.5k355134
42.5k355134
$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$
– BrainOverflow
May 26 '15 at 9:31
$begingroup$
@BrainOverflow I'd say most people agree that2^-3*4
is $2^{-3}cdot 4 = frac12$ and not $2^{-3cdot 4} = 2^{-12}$.
$endgroup$
– AlexR
May 26 '15 at 9:37
$begingroup$
@AlexR yeah. I think so. And what about2^-3^4
? This one is giving me a terrible headache. I'd say that it equals to(2^(-3))^4
.
$endgroup$
– BrainOverflow
May 26 '15 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 '15 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 '15 at 11:22
|
show 4 more comments
$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$
– BrainOverflow
May 26 '15 at 9:31
$begingroup$
@BrainOverflow I'd say most people agree that2^-3*4
is $2^{-3}cdot 4 = frac12$ and not $2^{-3cdot 4} = 2^{-12}$.
$endgroup$
– AlexR
May 26 '15 at 9:37
$begingroup$
@AlexR yeah. I think so. And what about2^-3^4
? This one is giving me a terrible headache. I'd say that it equals to(2^(-3))^4
.
$endgroup$
– BrainOverflow
May 26 '15 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 '15 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 '15 at 11:22
$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$
– BrainOverflow
May 26 '15 at 9:31
$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$
– BrainOverflow
May 26 '15 at 9:31
$begingroup$
@BrainOverflow I'd say most people agree that
2^-3*4
is $2^{-3}cdot 4 = frac12$ and not $2^{-3cdot 4} = 2^{-12}$.$endgroup$
– AlexR
May 26 '15 at 9:37
$begingroup$
@BrainOverflow I'd say most people agree that
2^-3*4
is $2^{-3}cdot 4 = frac12$ and not $2^{-3cdot 4} = 2^{-12}$.$endgroup$
– AlexR
May 26 '15 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$
– BrainOverflow
May 26 '15 at 9:41
$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$
– BrainOverflow
May 26 '15 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 '15 at 10:20
$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 '15 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 '15 at 11:22
$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 '15 at 11:22
|
show 4 more comments
$begingroup$
The numerical results I give below are consistent with Julia language, the latest and most sophisticated language for scientific computing, created by a team of MIT team involving mathematicians. All computations are also consistent with Google. Good for Google! When there are more than one equal signs, i.e, more than three members, in an expression the computations in Julia and Google are made with the first member, which states the problem, so I am not cheating.
In regular mathematical writing,
a^b^c = a^(b^c). [a^b^c in not equal to (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 negation operator, i.e., the unary -
operator that changes the sign of the expression that follows. It can be distinguished from the binary subtraction operator because the negation operator does not have a minuend just to the left.
Rule 1: The negation operator has the same level of precedence as exponentiation.
Rule 2: At this level, the operators of Rule 1 will be solved from right to left. Examples: a^-b = a^(-b) and -c^d = -(c^d). Numerical examples: 2^-3 = 2^(-3) = 1/8 and -2^3 = -(2^3) = -8.
Following the previous two rules, you can solve all the required computations:
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
2^-3^4 = 2^(-(3^4)) = 2^(-81) = 1 / 2^81 = a positive number very close to 0
4^-1^2 = 4^(-(1^2)) = 4^(-1) = 1/4
The second last example was posed in a comment. 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.]
As an alternative with equal results to Rules 1 and 2, one can state the following rules:
- Exponentiation has precedence over negation, as the documentation of mathematically reasonable languages typically state.
- Exponentiation operators are computed from right to left.
- Negation operators are computed from right to left.
- (following @TonyK's answer) The consecutive operators in
a^-b
can only be interpreted one way, because the minus sign is next tob
, and the exponentiation sign isn't. Therefore, operators precedence need not be considered.
Thanks to @BrainOverflow for telling us about Google. It uses the right mathematical conventions unlike Matlab and Excel for a^b^c, and unlike Excel for -a^(2*n), where n is an integer.
$endgroup$
$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, onlybc
seems to disagree on precedence of unary negation.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 18:26
$begingroup$
@DanielSchepler Thanks for the feedback. What about 2^1^2?
$endgroup$
– Rodolfo Oviedo
Dec 20 '18 at 21:53
$begingroup$
Gnuplot, Maxima and bc return 2 while Octave returns 4.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 21:57
$begingroup$
@DanielSchepler Many thanks! Excel returns 4. Google Sheets returns 2.
$endgroup$
– Rodolfo Oviedo
Dec 21 '18 at 6:44
add a comment |
$begingroup$
The numerical results I give below are consistent with Julia language, the latest and most sophisticated language for scientific computing, created by a team of MIT team involving mathematicians. All computations are also consistent with Google. Good for Google! When there are more than one equal signs, i.e, more than three members, in an expression the computations in Julia and Google are made with the first member, which states the problem, so I am not cheating.
In regular mathematical writing,
a^b^c = a^(b^c). [a^b^c in not equal to (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 negation operator, i.e., the unary -
operator that changes the sign of the expression that follows. It can be distinguished from the binary subtraction operator because the negation operator does not have a minuend just to the left.
Rule 1: The negation operator has the same level of precedence as exponentiation.
Rule 2: At this level, the operators of Rule 1 will be solved from right to left. Examples: a^-b = a^(-b) and -c^d = -(c^d). Numerical examples: 2^-3 = 2^(-3) = 1/8 and -2^3 = -(2^3) = -8.
Following the previous two rules, you can solve all the required computations:
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
2^-3^4 = 2^(-(3^4)) = 2^(-81) = 1 / 2^81 = a positive number very close to 0
4^-1^2 = 4^(-(1^2)) = 4^(-1) = 1/4
The second last example was posed in a comment. 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.]
As an alternative with equal results to Rules 1 and 2, one can state the following rules:
- Exponentiation has precedence over negation, as the documentation of mathematically reasonable languages typically state.
- Exponentiation operators are computed from right to left.
- Negation operators are computed from right to left.
- (following @TonyK's answer) The consecutive operators in
a^-b
can only be interpreted one way, because the minus sign is next tob
, and the exponentiation sign isn't. Therefore, operators precedence need not be considered.
Thanks to @BrainOverflow for telling us about Google. It uses the right mathematical conventions unlike Matlab and Excel for a^b^c, and unlike Excel for -a^(2*n), where n is an integer.
$endgroup$
$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, onlybc
seems to disagree on precedence of unary negation.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 18:26
$begingroup$
@DanielSchepler Thanks for the feedback. What about 2^1^2?
$endgroup$
– Rodolfo Oviedo
Dec 20 '18 at 21:53
$begingroup$
Gnuplot, Maxima and bc return 2 while Octave returns 4.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 21:57
$begingroup$
@DanielSchepler Many thanks! Excel returns 4. Google Sheets returns 2.
$endgroup$
– Rodolfo Oviedo
Dec 21 '18 at 6:44
add a comment |
$begingroup$
The numerical results I give below are consistent with Julia language, the latest and most sophisticated language for scientific computing, created by a team of MIT team involving mathematicians. All computations are also consistent with Google. Good for Google! When there are more than one equal signs, i.e, more than three members, in an expression the computations in Julia and Google are made with the first member, which states the problem, so I am not cheating.
In regular mathematical writing,
a^b^c = a^(b^c). [a^b^c in not equal to (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 negation operator, i.e., the unary -
operator that changes the sign of the expression that follows. It can be distinguished from the binary subtraction operator because the negation operator does not have a minuend just to the left.
Rule 1: The negation operator has the same level of precedence as exponentiation.
Rule 2: At this level, the operators of Rule 1 will be solved from right to left. Examples: a^-b = a^(-b) and -c^d = -(c^d). Numerical examples: 2^-3 = 2^(-3) = 1/8 and -2^3 = -(2^3) = -8.
Following the previous two rules, you can solve all the required computations:
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
2^-3^4 = 2^(-(3^4)) = 2^(-81) = 1 / 2^81 = a positive number very close to 0
4^-1^2 = 4^(-(1^2)) = 4^(-1) = 1/4
The second last example was posed in a comment. 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.]
As an alternative with equal results to Rules 1 and 2, one can state the following rules:
- Exponentiation has precedence over negation, as the documentation of mathematically reasonable languages typically state.
- Exponentiation operators are computed from right to left.
- Negation operators are computed from right to left.
- (following @TonyK's answer) The consecutive operators in
a^-b
can only be interpreted one way, because the minus sign is next tob
, and the exponentiation sign isn't. Therefore, operators precedence need not be considered.
Thanks to @BrainOverflow for telling us about Google. It uses the right mathematical conventions unlike Matlab and Excel for a^b^c, and unlike Excel for -a^(2*n), where n is an integer.
$endgroup$
The numerical results I give below are consistent with Julia language, the latest and most sophisticated language for scientific computing, created by a team of MIT team involving mathematicians. All computations are also consistent with Google. Good for Google! When there are more than one equal signs, i.e, more than three members, in an expression the computations in Julia and Google are made with the first member, which states the problem, so I am not cheating.
In regular mathematical writing,
a^b^c = a^(b^c). [a^b^c in not equal to (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 negation operator, i.e., the unary -
operator that changes the sign of the expression that follows. It can be distinguished from the binary subtraction operator because the negation operator does not have a minuend just to the left.
Rule 1: The negation operator has the same level of precedence as exponentiation.
Rule 2: At this level, the operators of Rule 1 will be solved from right to left. Examples: a^-b = a^(-b) and -c^d = -(c^d). Numerical examples: 2^-3 = 2^(-3) = 1/8 and -2^3 = -(2^3) = -8.
Following the previous two rules, you can solve all the required computations:
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
2^-3^4 = 2^(-(3^4)) = 2^(-81) = 1 / 2^81 = a positive number very close to 0
4^-1^2 = 4^(-(1^2)) = 4^(-1) = 1/4
The second last example was posed in a comment. 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.]
As an alternative with equal results to Rules 1 and 2, one can state the following rules:
- Exponentiation has precedence over negation, as the documentation of mathematically reasonable languages typically state.
- Exponentiation operators are computed from right to left.
- Negation operators are computed from right to left.
- (following @TonyK's answer) The consecutive operators in
a^-b
can only be interpreted one way, because the minus sign is next tob
, and the exponentiation sign isn't. Therefore, operators precedence need not be considered.
Thanks to @BrainOverflow for telling us about Google. It uses the right mathematical conventions unlike Matlab and Excel for a^b^c, and unlike Excel for -a^(2*n), where n is an integer.
edited Dec 21 '18 at 0:40
answered Dec 20 '18 at 17:50
Rodolfo OviedoRodolfo Oviedo
1034
1034
$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, onlybc
seems to disagree on precedence of unary negation.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 18:26
$begingroup$
@DanielSchepler Thanks for the feedback. What about 2^1^2?
$endgroup$
– Rodolfo Oviedo
Dec 20 '18 at 21:53
$begingroup$
Gnuplot, Maxima and bc return 2 while Octave returns 4.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 21:57
$begingroup$
@DanielSchepler Many thanks! Excel returns 4. Google Sheets returns 2.
$endgroup$
– Rodolfo Oviedo
Dec 21 '18 at 6:44
add a comment |
$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, onlybc
seems to disagree on precedence of unary negation.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 18:26
$begingroup$
@DanielSchepler Thanks for the feedback. What about 2^1^2?
$endgroup$
– Rodolfo Oviedo
Dec 20 '18 at 21:53
$begingroup$
Gnuplot, Maxima and bc return 2 while Octave returns 4.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 21:57
$begingroup$
@DanielSchepler Many thanks! Excel returns 4. Google Sheets returns 2.
$endgroup$
– Rodolfo Oviedo
Dec 21 '18 at 6:44
$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$
– Daniel Schepler
Dec 20 '18 at 18:26
$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$
– Daniel Schepler
Dec 20 '18 at 18:26
$begingroup$
@DanielSchepler Thanks for the feedback. What about 2^1^2?
$endgroup$
– Rodolfo Oviedo
Dec 20 '18 at 21:53
$begingroup$
@DanielSchepler Thanks for the feedback. What about 2^1^2?
$endgroup$
– Rodolfo Oviedo
Dec 20 '18 at 21:53
$begingroup$
Gnuplot, Maxima and bc return 2 while Octave returns 4.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 21:57
$begingroup$
Gnuplot, Maxima and bc return 2 while Octave returns 4.
$endgroup$
– Daniel Schepler
Dec 20 '18 at 21:57
$begingroup$
@DanielSchepler Many thanks! Excel returns 4. Google Sheets returns 2.
$endgroup$
– Rodolfo Oviedo
Dec 21 '18 at 6:44
$begingroup$
@DanielSchepler Many thanks! Excel returns 4. Google Sheets returns 2.
$endgroup$
– Rodolfo Oviedo
Dec 21 '18 at 6:44
add a comment |
$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$
add a comment |
$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$
add a comment |
$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$
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.
answered Dec 22 '18 at 13:03
badjohnbadjohn
4,2771620
4,2771620
add a comment |
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f1299236%2fwhy-does-unary-minus-operator-sometimes-take-precedence-over-exponentiation-and%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$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 '18 at 19:13