# Why do we need/use operator precedence for Arithmetic operators?

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.

• It’s a matter of pure convention. There’s nothing beyond that. Commented Jan 6, 2020 at 18:27
• 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. Commented Jan 6, 2020 at 21:28
• @Mars With all due respect, I have a feeling that you are explaining convention by convention. Commented Oct 8, 2023 at 9:23
• @smwikipedia, yes, you're right. I am explaining CS convention with mathematical convention. I think there's a meaningful difference since mathematical conventions developed over decades, and in some cases centuries, before computer languages were developed. And these days more people learn basic mathematical notation than learn programming (even though many people use very little math after they get out of high school. But still, that is to explain a convention in terms of another convention, exactly as you say.
– Mars
Commented Oct 9, 2023 at 5:00
• Commented Apr 17 at 17:43

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.

• 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.
– rici
Commented Jan 6, 2020 at 20:27
• @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. Commented Jan 6, 2020 at 20:56
• 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...
– rici
Commented Jan 6, 2020 at 21:11
• @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. Commented Jan 6, 2020 at 21:37