# Operator precedence

As operator precedence , -wertigkeit , -Priority or -präzedenz is called in mathematics , logic and computer science , a defined partial order in which the operators of an in infix notation this expression to be evaluated.

It is not a total order, but a partial order, because there is no strict order between all operators. There can also be several of the same rank. For example, the rank of multiplication and division is the same, but higher than the rank of addition and subtraction (" point calculation before line calculation "). You can save explicit brackets through the ranking. So is synonymous with because the multiplication operator has a higher rank. However, this is always a matter of definition. ${\ displaystyle a + b \ cdot c}$${\ displaystyle a + (b \ cdot c)}$

A bracketing offers the possibility of override of a section of a chain of listed operations: The bracketed, ie a set of parentheses "(...)" enclosed area is computationally first run and replace it with the corresponding partial result, making the brackets obsolete, as it no longer contains any operators. The brackets must contain the operators and their necessary operands.

If it is noted, the expression in brackets must first be calculated, i.e. the sum must be formed before multiplying with this sum. ${\ displaystyle a \ cdot (b + c)}$${\ displaystyle (b + c)}$${\ displaystyle a}$

Strictly speaking, the additional convention is required that expressions must be evaluated from left to right in order to determine the exact calculation sequence.

## Priority of different operators

For the standard arithmetic operations in mathematics, the following order of precedence is usual (in descending order of priority):

1. Exponentiation
2. Multiplication and division ("point calculation")
3. Addition and subtraction ("line calculation")

There are additional categories in programming languages ​​and computer programs for evaluating formulas (e.g. the Unix utility program bc ). One of them is the sign , which usually has an even higher priority than exponentiation. In mathematical formulas the expression is read as , but in the expressions of such evaluation programs it is often read as . ${\ displaystyle -a ^ {b}}$${\ displaystyle - (a ^ {b})}$${\ displaystyle (-a) ^ {b}}$

It is not always common in logic to define an operator precedence. Where this happens, the following is usually chosen (in descending priority):

After applying the mentioned operator precedence z. B. the arithmetic expression evaluated as , the logical expression as . ${\ displaystyle 3 + 4 \ cdot 5 ^ {- 6}}$${\ displaystyle 3+ (4 \ cdot (5 ^ {(- 6)}))}$${\ displaystyle P \ leftrightarrow Q \ rightarrow R \ lor S \ land \ neg T}$${\ displaystyle P \ leftrightarrow (Q \ rightarrow (R \ lor (S \ land (\ neg T)))))}$

## Order of equivalent operators

In addition, an associativity can be specified for operations , which determines the order in which adjacent, equivalent operators are to be evaluated. An operator is called left-associative if A op B op C op D is evaluated as ((A op B) op C) op D; an operator is called right-associative if A op B op C op D is evaluated as A op (B op (C op D)). Of the arithmetic operators mentioned, the exponentiation is defined as right-associative, i.e. H.:

${\ displaystyle a ^ {b ^ {c ^ {d}}} \ = \ a ^ {\ left (b ^ {\ left (c ^ {d} \ right)} \ right)} \ \ neq \ \ left (\ left (a ^ {b} \ right) ^ {c} \ right) ^ {d}}$.

Likewise the arrow operator :

${\ displaystyle a \ uparrow \ uparrow b \ uparrow \ uparrow c = a \ uparrow \ uparrow \ left (b \ uparrow \ uparrow c \ right) \ neq \ left (a \ uparrow \ uparrow b \ right) \ uparrow \ uparrow c}$

The remaining two-digit operators are defined as left-associative, i.e. H. for example . ${\ displaystyle ABCD = ((AB) -C) -D}$

In the logic connectives usually defined left-associative, but there are certainly also authors who use at least the conditional right-associative.

There are also programming languages ​​such as B. Occam , which put all operators on the same rank and evaluate from left to right.

## Outline signs

In order to change the defined operator precedence within an expression and to make expressions unique if a precedence is not defined, different forms of outline characters are used. In mathematics and also in modern logic, these are brackets.

In the history of logic, punctuation was often used, especially at the beginning, in order to prevent the ambiguity of expressions or to change a possibly defined operator precedence. A puncturing system, which gained a certain distribution through the basic work Principia Mathematica , was to be found more frequently in introductory logic books in variously modified forms until the 1970s. In this system, instead of the imaginary closing bracket before a juncture or instead of the imaginary opening bracket after a junctor, a period is placed. For example would be held

${\ displaystyle (A \ lor B) \ land (C \ lor D)}$

the following expression is written:

${\ displaystyle A \ lor B. \ land .C \ lor D}$

If several imaginary brackets meet, two or more periods are set instead of a single point, so that instead of, for example

${\ displaystyle ((A \ rightarrow B) \ lor C) \ land D}$

the following expression would be written:

${\ displaystyle A \ rightarrow B. \ lor C: \ land D}$

Another system weights the junctures occurring in an expression with points. The more points there are above an operator, the weaker its bond strength, that is, the lower its priority. According to this system, for example, a conjunction with two points would bind weaker than a conjunction with a point, which in turn would be weaker than a conjunction without points.

## Others

In programming languages , a weighting and associativity are usually specified for all operators so that the structure of all expressions is unique even if no parentheses are used. The evaluation of an expression without side effects therefore always gives the same value.

Operator precedence is usually only important for expressions in infix notation . Spellings like the Polish notation or the reverse Polish notation were developed so that the expressions are unambiguous without precedence and without brackets. Even with the first predicate logic notation, the conceptual notation and with graphical notations such as the existential graph , the reading of an expression is clearly defined and does not require any structural symbols or conventions.

In programming languages ​​that allow side effects in expressions, the order in which they are carried out or take effect is important. Some programming languages ​​strictly define this evaluation sequence, others (such as C or C ++ ) leave the evaluation sequence undefined for most of the Infix operands. Example:

int f1(void);
int f2(void);
int f3(int);

int g(void) { return f3( f1() * f2() ); }


It is not defined whether f1 or f2 is executed first (and thus "side effects" occur). In contrast, it is stipulated that f3 is only called after f1 and f2 (with their "side effects") have ended.

## Individual evidence

1. Jürgen Weiß: Taschenbuch der Mathematik ( Verlag Harri Deutsch and BG Teubner Verlagsgesellschaft ), ISBN 3-87144-492-8 , Volume 1, pp. 115-120, Chapter 2.4.1.1
2. ^ George Mark Bergman: Order of arithmetic operations
3. ^ Education Place: The Order of Operations