0
$\begingroup$

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:

{7.3`,7.199999999999999`}

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.

$\endgroup$
4
  • $\begingroup$ Closely related questions: mathematica.stackexchange.com/q/4818/3066 mathematica.stackexchange.com/q/5580/3066 $\endgroup$ – m_goldberg May 19 '14 at 14:05
  • $\begingroup$ 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. $\endgroup$ – Ivan May 19 '14 at 14:18
  • $\begingroup$ 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 $\endgroup$ – Daniel Lichtblau May 19 '14 at 14:37
  • $\begingroup$ Ok, I think this is it. Thanks! $\endgroup$ – Ivan May 19 '14 at 14:53
0
$\begingroup$

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'

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ – Ivan May 19 '14 at 14:13
  • $\begingroup$ Check the Help file for floating point arithmetic, and, on passing, check the concepts Accuracy and Precission. $\endgroup$ – Wouter May 19 '14 at 15:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.