# Table Command Not Working as Expected

Posted 9 years ago
2370 Views
|
3 Replies
|
2 Total Likes
|
 This may be a very basic question, but it is driving me nuts. I've created a minimal example that demonstrates the issue.I'm creating a list of numbers (to use as Ticks in a plot) as follows:Table[y, {y, -0.50, 0.50, 0.10}]That works exactly as I would expect, passing through exactly zero, giving the output below:In[291]:=  Table[y, {y, -0.50, 0.50, 0.10}]Out[291]= {-0.5, -0.4, -0.3, -0.2, -0.1, 0., 0.1, 0.2, 0.3, 0.4, 0.5}However, this does not work as expected: Table[y, {y, -0.60, 0.60, 0.10}]That one does not pass through zero, and I don't understand why. Here is the output:In[292]:=  Table[y, {y, -0.60, 0.60, 0.10}]Out[292]= {-0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 1.11022*10^-16, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6}I know that I can fix that by using Round[] as follows:Table[N[Round[y, 10^-4]], {y, -0.60, 0.60, 0.10}]and here is the output (note that it seems that N[] is required to avoid fractions):In[295]:=  Table[N[Round[y, 10^-4]], {y, -0.60, 0.60, 0.10}]Out[295]= {-0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6}So, my question is why, when you go from -0.5 to 0.5 in steps of 0.10 does it pass through 0, but when you go from -0.6 to 0.6 in steps of 0.10 it does not? Seems to me that I should get the output shown in the last box without having to use Round[]. Attachments:
3 Replies
Sort By:
Posted 9 years ago
 Thanks guys, that makes perfect sense even though it never would have occurred to me.
Posted 9 years ago
 Exactly -- I agree with David. I suspect the 0.5 case generated a "more exact" result because it is a power of 2. In[5]:= Table[y, {y, -0.60, 0.60, 0.10}]  Out[5]= {-0.6, -0.5, -0.4, -0.3, -0.2, -0.1,   1.11022*10^-16, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6}  In[6]:= % // Chop  Out[6]= {-0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4, \ 0.5, 0.6}In[7]:= Table[y, {y, -6/10, 6/10, 1/10}]Out[7]= {-(3/5), -(1/2), -(2/5), -(3/10), -(1/5), -(1/  10), 0, 1/10, 1/5, 3/10, 2/5, 1/2, 3/5}In[8]:= % // NOut[8]= {-0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0., 0.1, 0.2, 0.3, 0.4, \0.5, 0.6}
Posted 9 years ago
 Remember that with a command likeTable[y, {y, -0.50, 0.50, 0.10}]or in fact any command that is using floating point numbers, the numbers that are actually being used internally are the floating point representaiton of those numbers: not necessarilly what you see on the screen.  Thus you have the following going on internally in the computation for the 0.6 and the 0.5 case respectively: In[1]:= InputForm[6*0.1] Out[1]//InputForm= 0.6000000000000001   In[2]:= InputForm[5*0.1]   Out[2]//InputForm=0.5