Let x, y, and z be floating point numbers. Then is it true that (x+y)+z =/= x+(y+z)? Can someone give me an example? I want x,y, and z be floating point numbers (IEEE representation), not just any real numbers.
Asked
Active
Viewed 1,087 times
0
-
To make such an example, you can use a large number `x`, and choose `y` and `z` small enough that `x+y` and `x+z` both round down to `x`, but large enough that `x + (y+z)` rounds to `x + eps` where eps is the smallest increment that can be represented on the *scale* of `y` (which is a function of the exponent of `x`, the rounding mode of the floating point unit, and the bit-length of the mantissa on the particular hardware architecture.) – lockcmpxchg8b Dec 15 '17 at 04:04
-
This question is not exact duplicate because 0.1 cannot be exactly represented as IEEE FP. – Ted Dec 15 '17 at 04:22
-
@Ted: The numbers that .1, .2, and .7 round to when converted to IEEE-754 are representable, of course, and it is those values that demonstrate the failure of associativity. The failure occurs entirely within IEEE-754 operations. The initial representation of them with decimal plays no role in the demonstration. – Eric Postpischil Dec 15 '17 at 04:47
1 Answers
5
As you phrased the question, it is not generally true. There exist x, y, and z such that (x+y)+z = x+(y+z)
. But you likely intended to ask whether or not it is always true that (x+y)+z = x+(y+z)
. It is not always true.
An obvious counterexample is (264 + −264) + 1 and 264 + (−264 + 1). With 64-bit binary IEEE-754 arithmetic, the former is 0 + 1 = 1, and the latter is 264 + −264 = 0. In the latter, the 1 was lost because the magnitude of −264 is so great the sum did not have enough precision to include the 1.
There are more subtle cases, where just a part of one number is lost.

Eric Postpischil
- 195,579
- 13
- 168
- 312
-
One of the interesting features of your example is that all three inputs can be represented exactly in 64-bit binary IEEE-754. – Patricia Shanahan Dec 15 '17 at 04:29