0
$\begingroup$

Why do we use operator precedence rule for Arithmatic operators? Can't we simply just do the operation in a linear manner from left to right or vice-versa and deal with the operator that comes first. For e.g. 2+3*4 = (2+3)*4 , if we do the operation from left to right.

My guess why we might do it is that, since * and / (divide and multiply) are complicated and gets even more complicated if the operands are larger, whereas + and - (plus and subtract) are simple operations.

$\endgroup$
  • $\begingroup$ It’s a matter of pure convention. There’s nothing beyond that. $\endgroup$ – Yuval Filmus Jan 6 at 18:27
  • $\begingroup$ I feel this a matter of mathematical notation. I do not feel a CS angle beyond I am not particular about notations equivalent to the familiar one, and don't want to consider any that aren't. $\endgroup$ – greybeard Jan 6 at 21:28
  • $\begingroup$ I would add to "pure convention" and to Aaron Rotenberg's answer that these are conventions that are similar or compatible with to those that many people are used to using with mathematical notation outside of programming. It's unlikely that someone would guess that x + y ** z means (x + y)**z, because no one would interpret $x+y^z$ as $(x+y)^z$. (However, since I don't like to go and look up the convention for whatever language I am using that week, I tend to throw in parentheses everywhere, which is OK because I usually work alone and one of my favorite languages is Lisp.) $\endgroup$ – Mars Jan 7 at 1:04
3
$\begingroup$

As you note, there is no need for operator precedence. There are a number of other conventions that have been used in various contexts. A few programming languages have implemented the left-to-right convention you mention in the question, and a larger number have avoided the problem entirely by using Polish notation or its reverse.

The purpose of operator precedence conventions is to reduce the number of parentheses that are required to unambiguously communicate ideas that occur in practical mathematical usage. The most important rules—that multiplication has a higher precedence than addition, and exponentiation has a higher precedence than multiplication—exist primarily for the convenience of writing polynomials. Without these rules, polynomial expressions would require far more parentheses. Polynomials are so central to mathematical practice that they bleed into the notation, in operator precedence and elsewhere.

Rules for operator precedence beyond the basic arithmetic and relational ($=$, $\leq$, etc.) operators are domain-specific, and authors of papers occasionally have to specify the convention they are using in their notation section. Again, the purpose of having such rules is to improve communication in situations where specifying a convention that the reader has to remember is better than writing parentheses everywhere.

When precedence rules are standardized across an entire mathematical field, it is often because the rules have a clear analogy to arithmetic or relational operators on numbers. For example, concatenation has a higher precedence than union in regular expressions because the regular languages form a semiring, just like all your favorite number systems, and regular expressions are polynomial expressions in this semiring.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ With OP's proposed left-associative single-precedence notation you can write parenthesis-free polynomials: a*x+b*x+c*x…+d. Here the polynomial coefficients are a, b, c, …, d. That's arguably more straight-forward and certainly computationally more efficient than the way you are thinking about. $\endgroup$ – rici Jan 6 at 20:27
  • $\begingroup$ @rici Horner's rule, eh? That makes it a bit tricky to work out at a glance which exponents correspond to which coefficients, especially if you are omitting zero coefficients. Also, Horner's rule totally fails to address the non-numeric generalizations I mentioned such as regexes. That said, I can't tell you how the current precedence system actually came to be favored over other conventions—that's a question for HSM.SE, maybe. $\endgroup$ – Aaron Rotenberg Jan 6 at 20:56
  • $\begingroup$ I was really talking about representations. If you look at the Wikipedia page on Horner's method, you'll see a nice graphic which shows a parenthesis-free polynomial and hyper-parenthesized Hornerian equivalent. If you use precedence-free algebra, though, you end up moving all of the parentheses from one representation to the other. Both representations have their pros and cons; it's somewhat similar to the trade-offs between adjacency lists and adjacency matrices for representing graphs... $\endgroup$ – rici Jan 6 at 21:11
  • $\begingroup$ @rici When I say "polynomials" here, I'm really speaking very generally about sums of terms where the terms are products. This includes multivariable polynomials as well as many other things and extends far beyond what can be handled conveniently with "Hornerian" notation. The operator precedence convention reflects an opinion of the collective mathematical community that arranging an expression as a sum of products is inherently more natural than other equivalent ways of writing the same expression. $\endgroup$ – Aaron Rotenberg Jan 6 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.