2

I am encountering a floating point error with a very simplistic arithmetic problem in R:

17.1 - 5.9 - 11.2
5.9 + 11.2 - 17.1

yields 1.776357e-15 and 0, respectively. I've encountered these problems when I've had very large or very small numbers, but these are only non-zero to the tenths place.

Why am I getting different results?

This question is different than Why are these numbers not equal? because I am performing the same number of operations (2) in each equation, with the difference being a single plus/minus sign. I am curious why.

Thanks so much for your help in advance!

Oh, also using R v.3.5.1 on macOS v.10.13.6.

Chris Moore
  • 151
  • 6
  • 5
    Is it due to the precedence of operations `17.1 - (5.9 +11.2)# [1] 0` – akrun May 13 '19 at 12:47
  • @akrun Though the OP says it's not a dupe, isn't this a dupe? – Rui Barradas May 13 '19 at 13:49
  • @RuiBarradas The dupe in question is the site that I link to in the post. I explain why it's different. I would appreciate it if we discuss the similarity with me here than flagging it as a dupe. Thank you. – Chris Moore May 13 '19 at 14:13
  • There is not much to discuss. You may feel that it is a different issue, and you have explained the reason why you feel that, but it is in fact the same. Why would a different order of operations give a different result were it not due to floating point accuracy? – Rui Barradas May 13 '19 at 16:36
  • It's a different issue because in my case it's the **order** that is causing the problem, while the other site is very general. I would like help making the connection in my case, which isn't redundant. Yes, they reference 10 other examples, but none are specifically on the order in which these operations are computed. Order is not addressed in the other site, and I don't think that referring everything to one broad post is helpful to the person asking the question, which is what purportedly what SO aims to do, if I'm correct. – Chris Moore May 13 '19 at 16:48
  • Here is a key difference: When we compute `17.1-5.9`, the result is around 11.2, where the high bit of the significand is 8 (2^3), so the low bit is 2^-49. So the arithmetic has to round to the nearest 2^-49. When we compute `5.9+11.2`, the result is around 17.1, where the high bit is 16 (2^4), so the low bit is 2^-48. The limited significand width of the floating-point format forces these two results to round by different amounts. From that point on, the results differ. (Note: Even having the same exponents would not guarantee the results would be the same; there can be other causes.) – Eric Postpischil May 13 '19 at 17:17

1 Answers1

2

R groups equal precedence binary operators left to right.

17.1 - 5.9 - 11.2 is equivalent to (17.1 - 5.9) - 11.2

5.9 + 11.2 - 17.1 is equivalent to (5.9 + 11.2) - 17.1

The two expressions are much less similar than they look at first sight. The change in grouping is enough to cause different rounding errors.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75