问题
Why not simply evaluate Left to Right? Can someone explain how precedence makes code more readable? To me it seems to require more thought and more possibility for error. Einstein said, "Everything should be made as simple as possible, but no simpler." I guess he wasn't a programmer.
回答1:
because in Mathematics, before the invention of any computer language, there was operator precedence, e.g.
2 + 5 * 3 = 17 (and not 21)
A language not having a higher precedence for the *
operator than for the +
operator would generate confusion.
Another example from C++:
std::cout << 7 * 8 << std::endl;
If the *
operator did not have precedence over the <<
operator, this would not compile:
std::cout << 7
would be evaluated first, yieldingstd::cout
as a result (with the side effect that7
would be printed)- then one would want to evaluate
std::cout * 8
which -- unless somebody defines a really weird operator*
for multiplying anoutputstream
by an integer -- is not defined.
Operator precedence allows not having to put unnecessary parentheses 'within common sense' (although I agree that in some cases people introduce bugs because they are not aware of the actual precedences)
Now you might ask: why does Mathematics have operator precedence ? This wikipedia article mentions some historical facts and links to a discussion forum. For me, an imporant argument is the one that when writing polynomials, e.g.
ax^2 + bx + c
without operator precedence, one would have to write them as:
(a(x^2)) + (bx) + c
回答2:
The purpose of precedence is not to make it more readable, its to (a) standardize and (b) keep with standards set in mathematics, such as PEMDAS.
回答3:
APL does not have operator precedence (except parentheses); code reads right to left (like Hebrew or Arabic). That said, APL is a niche language and this is one reason why. Most other languages, as other answers already point out, mimic the long-established precedence rules of arithmetic, which in turn appear to me to arise from how typical use cases (i.e., real-world problems) can be modeled with a minimum number of parentheses.
回答4:
The digital world simply follows the old-school "real" world where the BEDMAS
rules: Brackets, Exponents, Division, Multiplication, Addition, Subtraction.
Better to follow the real world, than try to refedefine reality and end up with programmers doing
1 + 2 * 3
as
(1 + 2) * 3 = 9
v.s. a realworlder doing
1 + (2 * 3) = 7
and sending your multimillion dollar probe into an invalid atmospheric descent profile because someone "carried the 1" wrong.
回答5:
It is possible to do away with precedence rules - or more correctly, have just one such rule - as APL demonstrates.
"It has just one simple, consistent, and recursive precedence rule: the right argument of a function is the result of the entire expression to the right of that function."
For example, in "J" (an APL variant):
1+2*3 => 7 Computed as 1+(2*3)
2*3+1 => 8 Computed as 2*(3+1)
APL (and J) has so many operators, it would be difficult to remember the precedence and associativity rules of each and every one of them.
回答6:
The reason why there is an operator precedence is because you should see a sum like 2*4
as a WHOLE number.
So you could think of it in a way that 2*4
doesn't exist. It's simply 8
.
So what you're basically doing is "simplifying" the sum, like: 2+10*2
This basically says: 2+20
.
As in the following sum: 2143+8^2+6^9
. You don't want to write out 2143+262144+10077696
. (And imagine how this could look like if you have a much larger formula)
It's basically to simplify the look of the sum. And both addition and subtraction are the most basic form of operations. So that's why there is a precedence, it is to increase the readbility of the sum.
And like i said, since the +
and -
operators are the most basic operators there are, then it makes sense to first solve the more "complicated" operators.
回答7:
For example, with operator precedence the following evaluates to 14:
2 + 3 * 4
without it would evaluate to 20 which is unexpected, imho.
回答8:
Take an example , if there were no operator precedence how would you evaluate this expression :
1+2/3
Well , some would say that you want to divide the sum of 1+2
by 3
. Others that you want to add 2/3
to 1
!
来源:https://stackoverflow.com/questions/17154700/why-do-languages-have-operator-precedence