Home >Backend Development >C++ >Is Floating-Point Addition Associative?
The Associativity of Floating-Point Arithmetic
In the realm of programming, floating-point numbers are commonly used to represent real values. However, the fundamental operations of addition and multiplication on these floating-point values may not always behave as one expects.
A question that often arises is whether floating-point addition and multiplication are associative. Associativity implies that the order in which operands are added or multiplied does not affect the final result. While multiplication is indeed associative for floating-point numbers, addition is not.
This lack of associativity becomes apparent when observing the following code:
cout << ((0.7 + 0.2 + 0.1) == 1) << endl; //output is 0 cout << ((0.7 + 0.1 + 0.2) == 1) << endl; //output is 1
This puzzling behavior arises from the inherent imprecision of floating-point numbers. When adding multiple numbers, the order in which the additions are performed can lead to slightly different results due to rounding errors.
The seminal paper "What Every Computer Scientist Should Know about Floating Point Arithmetic" elucidates on this issue:
"Another grey area concerns the interpretation of parentheses. Due to roundoff errors, the associative laws of algebra do not necessarily hold for floating-point numbers. For example, the expression (x y) z has a totally different answer than x (y z) when x = 1e30, y = -1e30 and z = 1 (it is 1 in the former case, 0 in the latter). "
Therefore, one must be cautious when relying on the associativity of floating-point addition. The order in which operands are added can subtly alter the result, leading to unexpected behaviors.
The above is the detailed content of Is Floating-Point Addition Associative?. For more information, please follow other related articles on the PHP Chinese website!