HOME

TheInfoList



OR:

In
mathematics Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
and
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, the order of operations is a collection of rules that reflect conventions about which operations to perform first in order to evaluate a given
mathematical expression In mathematics, an expression is a written arrangement of symbols following the context-dependent, syntactic conventions of mathematical notation. Symbols can denote numbers, variables, operations, and functions. Other symbols include punct ...
. These rules are formalized with a ranking of the operations. The rank of an operation is called its precedence, and an operation with a ''higher'' precedence is performed before operations with ''lower'' precedence.
Calculator An electronic calculator is typically a portable electronic device used to perform calculations, ranging from basic arithmetic to complex mathematics. The first solid-state electronic calculator was created in the early 1960s. Pocket-si ...
s generally perform operations with the same precedence from left to right, but some
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s and calculators adopt different conventions. For example, multiplication is granted a higher precedence than addition, and it has been this way since the introduction of modern algebraic notation. Thus, in the expression , the multiplication is performed before addition, and the expression has the value , and not . When exponents were introduced in the 16th and 17th centuries, they were given precedence over both addition and multiplication and placed as a superscript to the right of their base. Thus and . These conventions exist to avoid notational
ambiguity Ambiguity is the type of meaning (linguistics), meaning in which a phrase, statement, or resolution is not explicitly defined, making for several interpretations; others describe it as a concept or statement that has no real reference. A com ...
while allowing notation to remain brief. Where it is desired to override the precedence conventions, or even simply to emphasize them, parentheses ( ) can be used. For example, forces addition to precede multiplication, while forces addition to precede
exponentiation In mathematics, exponentiation, denoted , is an operation (mathematics), operation involving two numbers: the ''base'', , and the ''exponent'' or ''power'', . When is a positive integer, exponentiation corresponds to repeated multiplication ...
. If multiple pairs of parentheses are required in a mathematical expression (such as in the case of nested parentheses), the parentheses may be replaced by other types of
bracket A bracket is either of two tall fore- or back-facing punctuation marks commonly used to isolate a segment of text or data from its surroundings. They come in four main pairs of shapes, as given in the box to the right, which also gives their n ...
s to avoid confusion, as in . These rules are meaningful only when the usual notation (called
infix notation Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands—"infixed operators"—such as the plus sign in . Usage Binary relations are ...
) is used. When functional or
Polish notation Polish notation (PN), also known as normal Polish notation (NPN), Łukasiewicz notation, Warsaw notation, Polish prefix notation, Eastern Notation or simply prefix notation, is a mathematical notation in which Operation (mathematics), operator ...
are used for all operations, the order of operations results from the notation itself.


Conventional order

The order of operations, that is, the order in which the operations in an expression are usually performed, results from a convention adopted throughout mathematics, science, technology and many computer
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s. It is summarized as: # Parentheses #
Exponentiation In mathematics, exponentiation, denoted , is an operation (mathematics), operation involving two numbers: the ''base'', , and the ''exponent'' or ''power'', . When is a positive integer, exponentiation corresponds to repeated multiplication ...
#
Multiplication Multiplication is one of the four elementary mathematical operations of arithmetic, with the other ones being addition, subtraction, and division (mathematics), division. The result of a multiplication operation is called a ''Product (mathem ...
and division #
Addition Addition (usually signified by the Plus and minus signs#Plus sign, plus symbol, +) is one of the four basic Operation (mathematics), operations of arithmetic, the other three being subtraction, multiplication, and Division (mathematics), divis ...
and
subtraction Subtraction (which is signified by the minus sign, –) is one of the four Arithmetic#Arithmetic operations, arithmetic operations along with addition, multiplication and Division (mathematics), division. Subtraction is an operation that repre ...
This means that to evaluate an expression, one first evaluates any sub-expression inside parentheses, working inside to outside if there is more than one set. Whether inside parentheses or not, the operation that is higher in the above list should be applied first. Operations of the same precedence are conventionally evaluated from left to right. If each division is replaced with multiplication by the reciprocal (multiplicative inverse) then the
associative In mathematics, the associative property is a property of some binary operations that rearranging the parentheses in an expression will not change the result. In propositional logic, associativity is a valid rule of replacement for express ...
and
commutative In mathematics, a binary operation is commutative if changing the order of the operands does not change the result. It is a fundamental property of many binary operations, and many mathematical proofs depend on it. Perhaps most familiar as a pr ...
laws of multiplication allow the factors in each term to be multiplied together in any order. Sometimes multiplication and division are given equal precedence, or sometimes multiplication is given higher precedence than division; see below. If each subtraction is replaced with addition of the opposite (additive inverse), then the associative and commutative laws of addition allow terms to be added in any order. The radical symbol is traditionally extended by a bar (called vinculum) over the radicand (this avoids the need for parentheses around 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, as in the case of and . Traditionally this convention extends to monomials; thus, and even , but , because is not a monomial. However, this convention is not universally understood, and some authors prefer explicit parentheses. Some calculators and programming languages require parentheses around function inputs, while others do not. Parentheses and alternate symbols of grouping can be used to override the usual order of operations or to make the intended order explicit. Grouped symbols can be treated as a single expression.


Examples

Multiplication before addition: : 1 + 2 \times 3 = 1 + 6 = 7 . Parenthetical subexpressions are evaluated first: : (1 + 2) \times 3 = 3 \times 3 = 9 . Exponentiation before multiplication, multiplication before subtraction: : 1 - 2 \times 3 ^ 4 = 1 - 2 \times 81 = 1 - 162 = -161 . When an expression is written as a superscript, the superscript is considered to be grouped by its position above its base: : 1+2^ = 1+2^7 = 1+128 = 129 . The operand of a root symbol is determined by the overbar: : \sqrt + 5 = \sqrt 4 + 5 = 2 + 5 = 7. A horizontal fractional line forms two grouped subexpressions, one above divided by another below: : \frac + 5 = \frac + 5. Parentheses can be nested, and should be evaluated from the inside outward. For legibility, outer parentheses can be made larger than inner parentheses. Alternately, other grouping symbols, such as curly braces or square brackets , are sometimes used along with parentheses . For example: : \bigl (1 + 2) \div (3 + 4) \bigr+ 5 = (3 \div 7) + 5


Special cases


Unary minus sign

There are differing conventions concerning the unary operation  (usually pronounced "minus"). In written or printed mathematics, the expression −32 is interpreted to mean . In some applications and programming languages, notably
Microsoft Excel Microsoft Excel is a spreadsheet editor developed by Microsoft for Microsoft Windows, Windows, macOS, Android (operating system), Android, iOS and iPadOS. It features calculation or computation capabilities, graphing tools, pivot tables, and a ...
, PlanMaker (and other spreadsheet applications) and the programming language bc, unary operations have a higher priority than binary operations, 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 for example in Microsoft Excel while the formulas =-2^2, =(-2)^2 and =0+-2^2 return 4, the formulas =0-2^2 and =-(2^2) return −4.


Mixed division and multiplication

There is no universal convention for interpreting an expression containing both division denoted by '÷' and multiplication denoted by '×'. Proposed conventions include assigning the operations equal precedence and evaluating them from left to right, or equivalently treating division as multiplication by the reciprocal and then evaluating in any order; evaluating all multiplications first followed by divisions from left to right; or eschewing such expressions and instead always disambiguating them by explicit parentheses. Beyond primary education, the symbol '÷' for division is seldom used, but is replaced by the use of
algebraic fraction In algebra, an algebraic fraction is a fraction whose numerator and denominator are algebraic expressions. Two examples of algebraic fractions are \frac and \frac. Algebraic fractions are subject to the same laws as arithmetic fractions. A ratio ...
s, typically written vertically with the numerator stacked above the denominator – which makes grouping explicit and unambiguous – but sometimes written inline using the slash or solidus symbol '/'. Multiplication denoted by juxtaposition (also known as implied multiplication) creates a visual unit and is often given higher precedence than most other operations. In academic literature, when inline fractions are combined with implied multiplication without explicit parentheses, the multiplication is conventionally interpreted as having higher precedence than division, so that e.g. is interpreted to mean rather than . For instance, the manuscript submission instructions for the '' Physical Review'' journals directly state that multiplication has precedence over division, and this is also the convention observed in physics textbooks such as the '' Course of Theoretical Physics'' by
Landau Landau (), officially Landau in der Pfalz (, ), is an autonomous (''kreisfrei'') town surrounded by the Südliche Weinstraße ("Southern Wine Route") district of southern Rhineland-Palatinate, Germany. It is a university town (since 1990), a long ...
and Lifshitz and mathematics textbooks such as '' Concrete Mathematics'' by Graham, Knuth, and Patashnik. However, some authors recommend against expressions such as , preferring the explicit use of parenthesis . More complicated cases are more ambiguous. For instance, the notation could plausibly mean either or . Sometimes interpretation depends on context. The ''Physical Review'' submission instructions recommend against expressions of the form ; more explicit expressions or are unambiguous. This ambiguity has been the subject of
Internet meme An Internet meme, or meme (, Help:Pronunciation respelling key, ''MEEM''), is a cultural item (such as an idea, behavior, or style) that spreads across the Internet, primarily through Social media, social media platforms. Internet memes manif ...
s such as "", for which there are two conflicting interpretations: 8 ÷   · (2 + 2)= 1 and (8 ÷ 2) · (2 + 2) = 16. Mathematics education researcher Hung-Hsi Wu points out that "one never gets a computation of this type in real life", and calls such contrived examples "a kind of Gotcha! parlor game designed to trap an unsuspecting person by phrasing it in terms of a set of unreasonably convoluted rules".


Serial exponentiation

If
exponentiation In mathematics, exponentiation, denoted , is an operation (mathematics), operation involving two numbers: the ''base'', , and the ''exponent'' or ''power'', . When is a positive integer, exponentiation corresponds to repeated multiplication ...
is indicated by stacked symbols using superscript notation, the usual rule is to work from the top down: : , which typically is not equal to (''a''''b'')''c''. This convention is useful because there is a property of exponentiation that (''a''''b'')''c'' = ''a''''bc'', so it's unnecessary to use serial exponentiation for this. However, when exponentiation is represented by an explicit symbol such as a
caret Caret () is the name used familiarly for the character provided on most QWERTY keyboards by typing . The symbol has a variety of uses in programming and mathematics. The name "caret" arose from its visual similarity to the original proofre ...
(^) or
arrow An arrow is a fin-stabilized projectile launched by a bow. A typical arrow usually consists of a long, stiff, straight shaft with a weighty (and usually sharp and pointed) arrowhead attached to the front end, multiple fin-like stabilizers c ...
(↑), there is no common standard. For example,
Microsoft Excel Microsoft Excel is a spreadsheet editor developed by Microsoft for Microsoft Windows, Windows, macOS, Android (operating system), Android, iOS and iPadOS. It features calculation or computation capabilities, graphing tools, pivot tables, and a ...
and computation programming language
MATLAB MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementat ...
evaluate ''a''^''b''^''c'' as (''a''''b'')''c'', but
Google Search Google Search (also known simply as Google or Google.com) is a search engine operated by Google. It allows users to search for information on the World Wide Web, Web by entering keywords or phrases. Google Search uses algorithms to analyze an ...
and Wolfram Alpha as ''a''(''b''''c''). Thus 4^3^2 is evaluated to 4,096 in the first case and to 262,144 in the second case.


Mnemonics

Mnemonic A mnemonic device ( ), memory trick or memory device is any learning technique that aids information retention or retrieval in the human memory, often by associating the information with something that is easier to remember. It makes use of e ...
acronyms are often taught in primary schools to help students remember the order of operations. The acronym ''PEMDAS'', which stands for Parentheses, Exponents, Multiplication/Division, Addition/Subtraction, is common in the
United States The United States of America (USA), also known as the United States (U.S.) or America, is a country primarily located in North America. It is a federal republic of 50 U.S. state, states and a federal capital district, Washington, D.C. The 48 ...
and France. Sometimes the letters are expanded into words of a mnemonic sentence such as "Please Excuse My Dear Aunt Sally". The United Kingdom and other Commonwealth countries may use ''BODMAS'' (or sometimes ''BOMDAS''), standing for Brackets, Of, Division/Multiplication, Addition/Subtraction, with "of" meaning fraction multiplication. Sometimes the O is instead expanded as Order, meaning exponent or root, or replaced by I for Indices in the alternative mnemonic ''BIDMAS''. In Canada and New Zealand ''BEDMAS'' is common. In Germany, the convention is simply taught as , "dot operations before line operations" referring to the graphical shapes of the taught operator signs , , and , . 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 a - b + c as a-(b+c), while the correct evaluation is (a - b) + c. These values are different when c\ne 0. Mnemonic acronyms have been criticized for not developing a conceptual understanding of the order of operations, and not addressing student questions about its purpose or flexibility. Students learning the order of operations via mnemonic acronyms routinely make mistakes, as do some pre-service teachers. Even when students correctly learn the acronym, a disproportionate focus on memorization of trivia crowds out substantive mathematical content. The acronym's procedural application does not match experts' intuitive understanding of mathematical notation: mathematical notation indicates groupings in ways other than parentheses or brackets and a mathematical expression is a tree-like hierarchy rather than a linearly "ordered" structure; furthermore, there is no single order by which mathematical expressions must be simplified or evaluated and no universal canonical simplification for any particular expression, and experts fluently apply valid transformations and substitutions in whatever order is convenient, so learning a rigid procedure can lead students to a misleading and limiting understanding of mathematical notation.


Calculators

Different calculators follow different orders of operations. Many simple calculators without a stack implement chain input, working in button-press order without any priority given to different operations, give a different result from that given by more sophisticated calculators. For example, on a simple calculator, typing 1 + 2 × 3 = yields 9, while a more sophisticated calculator will use a more standard priority, so typing 1 + 2 × 3 = yields 7. Calculators may associate exponents to the left or to the right. For example, the expression ''a''^''b''^''c'' is interpreted as ''a''(''b''''c'') on the TI-92 and the TI-30XS MultiView in "Mathprint mode", whereas it is interpreted as (''a''''b'')''c'' on the TI-30XII and the TI-30XS MultiView in "Classic mode". An expression like 1/2''x'' is interpreted as 1/(2''x'') by TI-82, as well as many modern
Casio is a Japanese multinational electronics manufacturing corporation headquartered in Shibuya, Tokyo, Japan. Its products include calculators, mobile phones, digital cameras, electronic musical instruments, and analogue and digital watches. It ...
calculators (configurable on some like the fx-9750GIII), but as (1/2)''x'' by TI-83 and every other TI calculator released since 1996, as well as by all
Hewlett-Packard The Hewlett-Packard Company, commonly shortened to Hewlett-Packard ( ) or HP, was an American multinational information technology company. It was founded by Bill Hewlett and David Packard in 1939 in a one-car garage in Palo Alto, California ...
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 rule that multiplication and division are of equal precedence. When the user is unsure how a calculator will interpret an expression, parentheses can be used to remove the ambiguity. Order of operations arose due to the adaptation of
infix notation Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands—"infixed operators"—such as the plus sign in . Usage Binary relations are ...
in standard mathematical notation, which can be notationally ambiguous without such conventions, as opposed to postfix notation or prefix notation, which do not need orders of operations. Hence, calculators utilizing reverse Polish notation (RPN) using a stack to enter expressions in the correct order of precedence do not need parentheses or any possibly model-specific order of execution.


Programming languages

Most programming languages use precedence levels that conform to the order commonly used in mathematics, though others, such as APL,
Smalltalk Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learni ...
, Occam and Mary, have no operator precedence rules (in APL, evaluation is strictly right to left; in Smalltalk, it is strictly left to right). Furthermore, 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 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 ("right associative"), e.g. in
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
, 1:2:3:4:[]

1:(2:(3:(4:[])))

[1,2,3,4]
. Dennis Ritchie, creator of the C (programming language), C language, said of the precedence in C (shared by programming languages that borrow those rules from C, for example, C++,
Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
and PHP) that it would have been preferable to move the bitwise operators above the comparison operators. Many programmers have become accustomed to this order, but more recent popular languages like Python and
Ruby Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
do have this order reversed. The relative precedence levels of operators found in many C-style languages are as follows: Examples: * !A + !B is interpreted as (!A) + (!B) * ++A + !B is interpreted as (++A) + (!B) * A + B * C is interpreted as A + (B * C) * A , , B && C is interpreted as A , , (B && C) * A && B

C
is interpreted as A && (B

C)
* A & B

C
is interpreted as A & (B

C)
(In Python,
Ruby Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
, PARI/GP and other popular languages, A & B

C
is interpreted as (A & B)

C
.)
Source-to-source compiler A source-to-source translator, source-to-source compiler (S2S compiler), transcompiler, or transpiler is a type of translator that takes the source code of a program written in a programming language as its input and produces an equivalent so ...
s that compile to multiple languages need to explicitly deal with the issue of different order of operations across languages.
Haxe Haxe is a high-level cross-platform programming language and compiler that can produce applications and source code for many different computing platforms from one code-base. It is free and open-source software, released under an MIT License. ...
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.


History

The order of operations emerged progressively over centuries. The rule that multiplication has precedence over addition was incorporated into the development of algebraic notation in the 1600s, since the distributive property implies this as a natural hierarchy. As recently as the 1920s, the historian of mathematics Florian Cajori identifies disagreement about whether multiplication should have precedence over division, or whether they should be treated equally. The term "order of operations" and the "PEMDAS/BEDMAS" mnemonics were formalized only in the late 19th or early 20th century, as demand for standardized textbooks grew. Ambiguity about issues such as whether implicit multiplication takes precedence over explicit multiplication and division in such expressions as ''a''/2''b'', which could be interpreted as ''a''/(2''b'') or (''a''/2) × ''b'', imply that the conventions are not yet completely stable.


See also

* Common operator notation (for a more formal description) * Hyperoperation * Logical connective#Order of precedence * Operator associativity * Operator overloading * Operator precedence in C and C++ *
Polish notation Polish notation (PN), also known as normal Polish notation (NPN), Łukasiewicz notation, Warsaw notation, Polish prefix notation, Eastern Notation or simply prefix notation, is a mathematical notation in which Operation (mathematics), operator ...
* Reverse Polish notation


Notes


References


Further reading

*


External links

* * Zachary, Joseph L. (1997) "Operator Precedence", supplement to ''Introduction to Scientific Programming''. University of Utah
Maple worksheet
{{DEFAULTSORT:Order Of Operations Mnemonics Operators (programming) Algebra