Within an expression containing two or more occurrences in a row of the same associative operator, the order in which the operations are performed does not matter as long as the sequence of the operands is not changed. That is (after rewriting the expression with parentheses and in infix notation if necessary), rearranging the parentheses in such an expression will not change its value. Consider the following equations:
Even though the parentheses were rearranged on each line, the values of the expressions were not altered. Since this holds true when performing addition and multiplication on any real numbers, it can be said that "addition and multiplication of real numbers are associative operations".
Associativity is not the same as commutativity, which addresses whether the order of two operands affects the result. For example, the order does not matter in the multiplication of real numbers, that is, a × b = b × a, so we say that the multiplication of real numbers is a commutative operation. However, operations such as function composition and matrix multiplication are associative, but (generally) not commutative.
Associative operations are abundant in mathematics; in fact, many algebraic structures (such as semigroups and categories) explicitly require their binary operations to be associative.
However, many important and interesting operations are non-associative; some examples include subtraction, exponentiation, and the vector cross product. In contrast to the theoretical properties of real numbers, the addition of floating point numbers in computer science is not associative, and the choice of how to associate an expression can have a significant effect on rounding error.
A binary operation ∗ on the set S is associative when this diagram commutes. That is, when the two paths from S×S×S to Scompose to the same function from S×S×S to S.
Formally, a binary operation ∗ on a setS is called associative if it satisfies the associative law:
(x ∗ y) ∗ z = x ∗ (y ∗ z) for all x, y, z in S.
Here, ∗ is used to replace the symbol of the operation, which may be any symbol, and even the absence of symbol (juxtaposition) as for multiplication.
(xy)z = x(yz) = xyz for all x, y, z in S.
The associative law can also be expressed in functional notation thus: f(f(x, y), z) = f(x, f(y, z)).
Generalized associative law
In the absence of the associative property, five factors a, b, c, d, e result in a Tamari lattice of order four, possibly different products.
If a binary operation is associative, repeated application of the operation produces the same result regardless of how valid pairs of parentheses are inserted in the expression. This is called the generalized associative law. For instance, a product of four elements may be written, without changing the order of the factors, in five possible ways:
If the product operation is associative, the generalized associative law says that all these formulas will yield the same result. So unless the formula with omitted parentheses already has a different meaning (see below), the parentheses can be considered unnecessary and "the" product can be written unambiguously as
An example where this does not work is the logical biconditional. It is associative, thus A(BC) is equivalent to (AB)C, but ABC most commonly means (AB and BC), which is not equivalent.
In associative operations is .
The addition of real numbers is associative.
Some examples of associative operations include the following.
The concatenation of the three strings "hello", " ", "world" can be computed by concatenating the first two strings (giving "hello ") and appending the third string ("world"), or by joining the second and third string (giving " world") and concatenating the first string ("hello") with the result. The two methods produce the same result; string concatenation is associative (but not commutative).
Because of associativity, the grouping parentheses can be omitted without ambiguity.
The trivial operation x ∗ y = x (that is, the result is the first argument, no matter what the second argument is) is associative but not commutative. Likewise, the trivial operation x ∘ y = y (that is, the result is the second argument, no matter what the first argument is) is associative but not commutative.
In mathematics, addition and multiplication of real numbers is associative. By contrast, in computer science, the addition and multiplication of floating point numbers is not associative, as rounding errors are introduced when dissimilar-sized values are joined together.
To illustrate this, consider a floating point representation with a 4-bit mantissa:
1.0012×24 1.0002×20 +
Even though most computers compute with a 24 or 53 bits of mantissa, this is an important source of rounding error, and approaches such as the Kahan summation algorithm are ways to minimise the errors. It can be especially problematic in parallel computing.
Notation for non-associative operations
In general, parentheses must be used to indicate the order of evaluation if a non-associative operation appears more than once in an expression (unless the notation specifies the order in another way, like ). However, mathematicians agree on a particular order of evaluation for several common non-associative operations. This is simply a notational convention to avoid parentheses.
A left-associative operation is a non-associative operation that is conventionally evaluated from left to right, i.e.,
while a right-associative operation is conventionally evaluated from right to left:
Both left-associative and right-associative operations occur. Left-associative operations include the following:
Exponentiation is commonly used with brackets or right-associatively because a repeated left-associative exponentiation operation is of little use. Repeated powers would mostly be rewritten with multiplication:
Formatted correctly, the superscript inherently behaves as a set of parentheses; e.g. in the expression the addition is performed before the exponentiation despite there being no explicit parentheses wrapped around it. Thus given an expression such as , the full exponent of the base is evaluated first. However, in some contexts, especially in handwriting, the difference between , and can be hard to see. In such a case, right-associativity is usually implied.
Hungerford, Thomas W. (1974). Algebra (1st ed.). Springer. p. 24. ISBN 978-0387905181. Definition 1.1 (i) a(bc) = (ab)c for all a, b, c in G.
^Durbin, John R. (1992). Modern Algebra: an Introduction (3rd ed.). New York: Wiley. p. 78. ISBN 978-0-471-51001-7. If are elements of a set with an associative operation, then the product is unambiguous; this is, the same element will be obtained regardless of how parentheses are inserted in the product
^"Matrix product associativity". Khan Academy. Retrieved 5 June 2016.
^Moore, Brooke Noel; Parker, Richard (2017). Critical Thinking (12th ed.). New York: McGraw-Hill Education. p. 321. ISBN 9781259690877.
^Copi, Irving M.; Cohen, Carl; McMahon, Kenneth (2014). Introduction to Logic (14th ed.). Essex: Pearson Education. p. 387. ISBN 9781292024820.
^Hurley, Patrick J.; Watson, Lori (2016). A Concise Introduction to Logic (13th ed.). Boston: Cengage Learning. p. 427. ISBN 9781305958098.
^"Symbolic Logic Proof of Associativity". Math.stackexchange.com. 22 March 2017.
^IEEE Computer Society (29 August 2008). IEEE Standard for Floating-Point Arithmetic. doi:10.1109/IEEESTD.2008.4610935. ISBN 978-0-7381-5753-5. IEEE Std 754-2008.
^Villa, Oreste; Chavarría-mir, Daniel; Gurumoorthi, Vidhya; Márquez, Andrés; Krishnamoorthy, Sriram, Effects of Floating-Point non-Associativity on Numerical Computations on Massively Multithreaded Systems(PDF), archived from the original (PDF) on 15 February 2013, retrieved 8 April 2014
^Goldberg, David (March 1991). "What Every Computer Scientist Should Know About Floating-Point Arithmetic" (PDF). ACM Computing Surveys. 23 (1): 5–48. doi:10.1145/103162.103163. S2CID 222008826. Retrieved 20 January 2016. (,  Archived 2016-04-06 at the Wayback Machine)
^George Mark Bergman: Order of arithmetic operations