Unexpected behavior of subtraction with approximate numbers


I'm using the Table command to generate a list of values:

FullForm /@ Table[10. - x, {x, 2.7, 2.8, .1}]

and I get the following result:


Why do I get 7.199999999999999' instead of 7.2?

Moreover, if I try to calculate the Complement of {7.2} in this list, I get {7.2,7.3}, which is not what I would expect. At the same time, using the Complement option SameTest->(#1==#2&) gets me {7.3}.

This behavior is observed in Mathematica 7/8/9, and even on WolframAlpha.

P.S. I know that using Range[7.3,7.2,-0.1] would be clearer, I'm just curious about the aforementioned problem.


Posted 2014-05-19T12:59:32.603

Reputation: 1

Question was closed 2014-05-19T16:22:52.147

Closely related questions: http://mathematica.stackexchange.com/q/4818/3066

– m_goldberg – 2014-05-19T14:05:29.257

It appears that the problem is not with Table but rather with substraction. The question is why 10.-(2.7+0.1)//FullForm and 10.-2.8//FullForm produce different results. – Ivan – 2014-05-19T14:18:29.440

The numbers in question are not exactly representable in base 2, which is the form used for machine arithmetic. So there will be discrepancies from round-off when one does arithmetic operations. The example in question: `InputForm[2.7+.1]

Out[8]//InputForm= 2.8000000000000003 ` – Daniel Lichtblau – 2014-05-19T14:37:42.507

Ok, I think this is it. Thanks! – Ivan – 2014-05-19T14:53:04.723



Why should it have anything to do with 'Table' ? Try 10. - (2.7 + 0.1) // FullForm it shows the same result, since you 'denude' the floating point residuals by 'Fullform'


Posted 2014-05-19T12:59:32.603

Reputation: 1 273

Ok, thanks for the example, it seems that Table is good, but now I don't understand why 10.-(2.7+0.1)//FullForm and 10.-2.8//FullForm produce different results. – Ivan – 2014-05-19T14:13:59.997

Check the Help file for floating point arithmetic, and, on passing, check the concepts Accuracy and Precission. – Wouter – 2014-05-19T15:00:57.140