Order of operations


In mathematics and computer programming, the order of operations is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression.
For example, in mathematics and most computer languages, multiplication is granted a higher precedence than addition, and it has been this way since the introduction of modern algebraic notation. Thus, the expression is interpreted to have the value, not. With the introduction of exponents in the 16th and 17th centuries, they were given precedence over both addition and multiplication and could be placed only as a superscript to the right of their base. Thus and.
These conventions exist to eliminate ambiguity while allowing notation to be as brief as possible. Where it is desired to override the precedence conventions, or even simply to emphasize them, parentheses can indicate an alternative order or reinforce the default order to avoid confusion. For example, forces addition to precede multiplication, and forces addition to precede exponentiation. Sometimes, for clarity, especially with nested parentheses, the parentheses are replaced by brackets, as in

Definition

The order of operations, which is used throughout mathematics, science, technology and many computer programming languages, is expressed here:
  1. exponentiation and root extraction
  2. multiplication and division
  3. addition and subtraction
This means that if, in a mathematical expression, a subexpression appears between two operators, the operator that is higher in the above list should be applied first.
The commutative and associative laws of addition and multiplication allow adding terms in any order, and multiplying factors in any order—but mixed operations must obey the standard order of operations.
In some contexts, it is helpful to replace a division by multiplication by the reciprocal and a subtraction by addition of the opposite. For example, in computer algebra, this allows manipulating fewer binary operations and makes it easier to use commutativity and associativity when simplifying large expressions – for more details, see. Thus ; in other words, the quotient of 3 and 4 equals the product of 3 and. Also ; in other words the difference of 3 and 4 equals the sum of 3 and −4. Thus, can be thought of as the sum of, and the three summands may be added in any order, in all cases giving 5 as the result.
The root symbol √ is traditionally prolongated by a bar over the radicand. Other functions use parentheses around the input to avoid ambiguity. The parentheses can be omitted if the input is a single numerical variable or constant, e.g. or. Another shortcut convention sometimes used is when the input is monomial; thus, rather than, but, because is not a monomial. This, however, is ambiguous and not universally understood outside of specific contexts.
Some calculators and programming languages require parentheses around function inputs, some do not.
Symbols of grouping can be used to override the usual order of operations. Grouped symbols can be treated as a single expression. Symbols of grouping can be removed using the associative and distributive laws, also they can be removed if the expression inside the symbol of grouping is sufficiently simplified so no ambiguity results from their removal.

Examples

A horizontal fractional line also acts as a symbol of grouping:
For ease in reading, other grouping symbols, such as curly braces or square brackets , are often used along with parentheses. For example:

Unary minus sign

There are differing conventions concerning the unary operator −. In written or printed mathematics, the expression −32 is interpreted to mean.
In some applications and programming languages, notably Microsoft Excel and the programming language bc, unary operators have a higher priority than binary operators, that is, the unary minus has higher precedence than exponentiation, so in those languages −32 will be interpreted as. This does not apply to the binary minus operator −; for example in Microsoft Excel while the formulas =-2^2, =-^2 and =0+-2^2 return 4, the formula =0-2^2 and =- return −4.

Mixed division and multiplication

Similarly, there can be ambiguity in the use of the slash symbol / in expressions such as 1/2x. If one rewrites this expression as and then interprets the division symbol as indicating multiplication by the reciprocal, this becomes:
With this interpretation is equal to. However, in some of the academic literature, multiplication denoted by juxtaposition is interpreted as having higher precedence than division, so that equals, not.
For example, the manuscript submission instructions for the Physical Review journals state that multiplication is of higher precedence than division with a slash, and this is also the convention observed in prominent physics textbooks such as the Course of Theoretical Physics by Landau and Lifshitz and the Feynman Lectures on Physics.

Mnemonics

are often used to help students remember the rules, involving the first letters of words representing various operations. Different mnemonics are in use in different countries.
These mnemonics may be misleading when written this way. For example, misinterpreting any of the above rules to mean "addition first, subtraction afterward" would incorrectly evaluate the expression
The correct value is 9.

Special cases

Serial exponentiation

If exponentiation is indicated by stacked symbols using superscript notation, the usual rule is to work from the top down:
which typically is not equal to c.
However, when using operator notation with a caret or arrow, there is no common standard. For example, Microsoft Excel and computation programming language MATLAB evaluate a^b^c as c, but Google Search and Wolfram Alpha as a. Thus 4^3^2 is evaluated to 4,096 in the first case and to 262,144 in the second case.

Serial division

A similar ambiguity exists in the case of serial division, for example, the expression can either be interpreted as
or as
The left-to-right operation convention would resolve the ambiguity in favor of the last expression. Further, the mathematical habit of combining factors and representing division as multiplication by a reciprocal both greatly reduce the frequency of ambiguous division.

Calculators

Different calculators follow different orders of operations. Many simple calculators without a stack implement chain input working left to right without any priority given to different operators, for example typing
while more sophisticated calculators will use a more standard priority, for example typing
The Microsoft Calculator program uses the former in its standard view and the latter in its scientific and programmer views.
Chain input expects two operands and an operator. When the next operator is pressed, the expression is immediately evaluated and the answer becomes the left hand of the next operator. Advanced calculators allow entry of the whole expression, grouped as necessary, and evaluates only when the user uses the equals sign.
Calculators may associate exponents to the left or to the right depending on the model or the evaluation mode. For example, the expression a^b^c is interpreted as a on the TI-92 and the TI-30XS MultiView in "Mathprint mode", whereas it is interpreted as c on the TI-30XII and the TI-30XS MultiView in "Classic mode".
An expression like 1/2x is interpreted as 1/ by TI-82, as well as many modern Casio calculators, but as x by TI-83 and every other TI calculator released since 1996, as well as by all Hewlett-Packard calculators with algebraic notation. While the first interpretation may be expected by some users due to the nature of implied multiplication, the latter is more in line with the standard rule that multiplication and division are of equal precedence, where 1/2x is read one divided by two and the answer multiplied by x.
When the user is unsure how a calculator will interpret an expression, it is a good idea to use parentheses so there is no ambiguity.
Calculators that utilize reverse Polish notation, also known as postfix notation, use a stack to enter expressions in the correct order of precedence without a need for parentheses or any possibly model-specific order of execution.

Programming languages

Some programming languages use precedence levels that conform to the order commonly used in mathematics, though others, such as APL, Smalltalk, Occam and Mary, have no operator precedence rules.
In addition, because many operators are not associative, the order within any single level is usually defined by grouping left to right so that 16/4/4 is interpreted as rather than ; such operators are perhaps misleadingly referred to as "left associative". Exceptions exist; for example, languages with operators corresponding to the cons operation on lists usually make them group right to left, e.g. in Haskell, 1:2:3:4: 1: .
The logical bitwise operators in C have a precedence level that the creator of the C language has since criticised as unsatisfactory. However, many programmers have become accustomed to this order. The relative precedence levels of operators found in many C-style languages are as follows:
1 -> . ::Function call, scope, array/member access
2! ~ - + * & sizeof type cast ++ -- unary operators, sizeof and type casts
3* / % MODMultiplication, division, modulo
4+ -Addition and subtraction
5<< >>Bitwise shift left and right
6< <= > >=Comparisons: less-than and greater-than
7 !=Comparisons: equal and not equal
8&Bitwise AND
9^Bitwise exclusive OR
10|Bitwise inclusive OR
11&&Logical AND
12||Logical OR
13? :Conditional expression
14= += -= *= /= %= &= |= ^= <<= >>=Assignment operators
15,Comma operator

Examples:
Source-to-source compilers that compile to multiple languages need to explicitly deal with the issue of different order of operations across languages. Haxe for example standardizes the order and enforces it by inserting brackets where it is appropriate.
The accuracy of software developer knowledge about binary operator precedence has been found to closely follow their frequency of occurrence in source code.