Pages

Wednesday, April 27, 2011

Order of operations

mathematics and computer programming, the order of operations is a rule used to clarify unambiguously which procedures should be performed first in a given mathematical expression.
For example, in mathematics and most computer languages multiplication is done before addition; in the expression 2 + 3 × 4, the algebraic answer is 14. Brackets, which have their own rules, may be used to avoid confusion, thus the above expression may also be rendered 2 + (3 × 4).
From the introduction of modern algebraic notation, where juxtaposition indicates multiplication of variables, multiplication took precedence over addition, whichever side of a number it appeared on.Thus 3 + 4 × 5 = 4 × 5 + 3 = 23. When exponents were first introduced, in the 16th and 17th centuries, exponents took precedence over both addition and multiplication, and could be placed only as a superscript to the right of their base. Thus 3 + 52 = 28 and 3 × 52 = 75. To change the order of operations, originally a vinculum (an overline or underline) was used. Today we use brackets. Thus, to force addition to precede multiplication, we write (2 + 3) × 4 = 20, and to force addition to precede exponentiation, we write (3 + 5)2 = 64.
Programming languages

Many programming languages use precedence levels that conform to the order commonly used in mathematics, though some, such as APL or Smalltalk, have no operator precedence rules (in APL evaluation is strictly right to left, in Smalltalk it's strictly left to right).
The logical bitwise operators in C (and all programming languages that borrowed precedence rules from C, for example, C++, Perl and PHP) have a precedence level that the creator of the C language considers to be unsatisfactory. However, many programmers have become accustomed to this order. The relative precedence levels of operators found in many C-style languages.
Calculators

Different calculators follow different orders of operations. Most non-scientific calculators without a stack work left to right without any priority given to different operators, for example giving

while more sophisticated calculators will use a more standard priority, for example giving

The Microsoft Calculator program uses the former in its standard view and the latter in its scientific view.
The non-scientific calculator 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 only evaluates when the user uses the equals sign.
Calculators may associate exponents to the left or to the right depending on the model. For example, the expression a ^ b ^ c on the TI-92 and TI-30XII (both Texas Instruments calculators) associates two different ways:
The TI-92 associates to the right, that is
a ^ b ^ c = a ^ (b ^ c) =
whereas, the TI-30XII associates to the left, that is
a ^ b ^ c = (a ^ b) ^ c = (ab)c.
An expression like 1/2x is interpreted as 1/(2x) by TI-82, but as (1/2)x by TI-83. While the first interpretation may be expected by some users, only the latter is in agreement with the standard rules stated above.

No comments:

Post a Comment