2

I note that python3 says that:

  • 1.1+2.2 is 3.3000000000000003.
  • 2.2+4.4 is 6.6000000000000005.
  • 1.1+2.3 is 3.4.
  • 2.2+4.3 is 6.5.
  • And so on.

I realise that floating point arithmetic is not perfectly accurate, as per

Is floating point math broken?

so that, for instance, that 1.1 may not be perfectly represented inside the float type.

However, there seems to be something about a query of the form x+2x that provokes an exceptional inaccuracy. Is there a simple intuition as to why?

Jim
  • 229
  • 1
  • 4
  • It is largely just coincidence. Python is concealing some errors from you; it is not printing all digits, just those it thinks it needs to show a value that is “close enough” to the actual value. If it printed all the digits needed to show the exact value that was calculated inside the computer, then you would see that 1.1+2.3 has some error, just as 1.1+2.2 does. The errors in 1.1 and 2.2 are only slightly larger than the errors in your other examples, just enough to trigger the criterion to print more digits. – Eric Postpischil Oct 24 '17 at 10:25
  • To explain further: In calculating 1.1+2.2, there are three errors caused by rounding. First, 1.1 is converted to internal format. Then 2.2 is converted to internal format. Then, when they are added, the exact mathematical result is rounded to internal format. If these three errors are all in the same direction, they produce a greater total error than if some of them happen to be in opposite directions. So, likely 1.1 rounds up a little, 2.2 rounds up a little, and their sum also happens to round up, so you get a lot of error. Likely 2.3 rounds down, so 1.1+2.3 gets some cancellation of error. – Eric Postpischil Oct 24 '17 at 10:28
  • I disagree that this question is an exact duplicate. The proposed original explains generally that floating-point arithmetic is an approximation. This question asks about specific errors. – Eric Postpischil Oct 24 '17 at 10:31

1 Answers1

0

According to Computer science circles Python uses approximations of decimal numbers. That's why certain equations which are mathematically true may not be true in Python.

Dogg1
  • 151
  • 1
  • 1
  • 13
  • Thank you. Yes. But why is is the error calculating x+2x noticeably greater than that calculating, say, x+4x? – Jim Oct 23 '17 at 14:16
  • I don't know that, but I think it's caused by binary64 format. – Dogg1 Oct 23 '17 at 14:53