Content deleted Content added
Standard notation. |
More to the point (better flow, coherence, correctness, and less redundancy). |
||
Line 2:
{{Unreferenced|date=August 2009}}
{{Cleanup-reorganize|date=July 2007}}
In [[programming languages]], [[scientific calculator]]s and similar '''common operator notation''' is
Operands are
Each operator is given a position, precedence, and an associativity. The
▲Operands are mathematical objects upon which the operators operate. These include [[number]]s such as 3 or 1001, [[truth value]]s such as true or false, structures such as vectors, or any other mathematical object. One special type of operand is the parenthesis group. An expression enclosed in parentheses is evaluated recursively and treated, for operator association purposes, as a single operand.
▲Each operator is given a position, precedence, and an associativity. The precedence is a number, and '''operator precedence''' is usually ordered with the corresponding number order, although some implementations give higher precedences lower numbers. In cases in which two operators of different precedences compete for the same operand, the operator with the higher precedence wins. For example, '×' has a higher precedence than '+', so 3+4×5 = (3+(4×5)), not ((3+4)×5).
[[Operator associativity]]
▲[[Operator position]] indicates where, in the sequence, the operator appears. In terms of operator position, an operator may be prefix, postfix, or infix. A prefix operator immediately precedes its operand, as in "−x". A postfix operator immediately succeeds its operand, as in "x!". An infix operator exists between its left and right operands, as in "A + B".
▲[[Operator associativity]], loosely speaking, describes what operators are allowed to associate before associating with the operator in question. In those cases in which operators of equal precedence compete for common operands, operator associativity describes the order of operator association. An infix operator can be left-associative, right-associative, or non-associative. A prefix or postfix operator can be either associative or non-associative. If an operator is left-associative, the operators are applied in left-to-right order. The arithmetic operators '+', '−', '×', and '÷', for example, are all left-associative. That is,
:<math>3+4+5-6-7 = ((((3+4)+5)-6)-7).</math>
Note that, contrary to popular belief, prefix and postfix operators do not necessarily have higher precedence than all infix operators. For example, if the prefix operator 'sin' is given a precedence between that of '+' and '×',▼
▲
:<math>\sin 4 \cdot 3+2 = ((\sin(4 \cdot 3)) + 2)</math>
not
:<math>(((\sin 4) \cdot 3) + 2)</math>
or
:<math>\sin(((4 \cdot 3)+2))</math>
The rules for expression evaluation are
# Treat any sub-expression in parentheses as a single recursively-evaluated operand (there may be different kinds of parentheses though, with different semantics).▼
# Bind operands to operators of higher precedence before those of lower precedence.
▲# Treat any sub-expression in parentheses as a single recursively-evaluated operand.
#
Examples:
:<tt>1 + 2 + 3 * 4 * 5 + 6 + 7 = ((((1 + 2) + ((3 * 4) * 5)) + 6) + 7)</tt>
|