# Table does not give expected result with 30 but right with 40

I want Mathematica to generate a table, but I am very confused by its behaviour.

tab = Table[i, {i, 6.311, 6.312, 0.001}]


Now I get the desired result, namely {6.311, 6.312}.

But in my actual code I want to solve a differential equation for different values of i to a very high precision. But when I do this

tab = Table[i, {i, 6.31130, 6.31230, 0.00130}]


The result that I get is {6.31100000000000000000000000000}, so only 1 value instead of two.

But this

tab = Table[i, {i, 6.31140, 6.31240, 0.00140}]


Gives the desired result again, namely {6.311000000000000000000000000000000000000,
6.312000000000000000000000000000000000000}.

I really have no clue what is happening, and I hope someone could enlighten me because I have been breaking my head about it.

• My first guess is floating-point round-off error causing trouble at the end point, but I can't really show it. It might be something else...Report it to WRI. (This also fails: Range[6.31130, 6.31230, 0.00130].) Mar 26 at 17:23
• Numbers are stored and evaluated as binary numbers. As a general rule, use exact numbers for known constants to minimize numeric representation issues. tab = Table[i, {i, 6311/1000, 6312/1000, 1/1000}] Mar 26 at 17:28
• @BobHanlon The binary representation didn't seem to be the issue here in my test. In fact, Table usually seems to compare with tolerance at the end point (e.g. Table[i, {i, 0, 3, 3*0.1}], even though 3 * 0.1 is a bit greater than 0.3 and the table end a bit past 3.) Mar 26 at 17:35
• @MichaelE2 - I wouldn't draw any conclusions about arbitrary-precision comparisons by looking at machine precision examples. "Approximate numbers with machine precision or higher are considered equal if they differ in at most their last seven binary digits (roughly their last two decimal digits)" I don't know how many guard bits are used/considered when working with arbitrary precision, but there are none with machine precision. Mar 26 at 17:44
• @Charlotte, Another possible workaround is Table[..., {i, Subdivide[a, b, n]}]. Mar 26 at 17:59

I don't know exactly what the problem is (so it seems to be a bug), but here are a couple of workarounds:

Block[{$MinPrecision = 30}, Table[i, {i, 6.31130, 6.31230, 0.00130}] ] (* {6.31100000000000000000000000000, 6.31200000000000000000000000000} *)  If you don't want to set the min precision for your whole code, you can isolate the setting for the iterator this way: Table[i, {i, Block[{$MinPrecision = 30},
Range[6.31130, 6.31230, 0.00130]]}
]

(*
{6.31100000000000000000000000000,
6.31200000000000000000000000000}
*)
`

And of course, one can use 40-digit precision as the OP observed.