6
$\begingroup$

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.311`30, 6.312`30, 0.001`30}]

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

But this

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

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.

$\endgroup$
6
  • 2
    $\begingroup$ 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.311`30, 6.312`30, 0.001`30].) $\endgroup$
    – Michael E2
    Mar 26 at 17:23
  • $\begingroup$ 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}] $\endgroup$
    – Bob Hanlon
    Mar 26 at 17:28
  • $\begingroup$ @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.) $\endgroup$
    – Michael E2
    Mar 26 at 17:35
  • $\begingroup$ @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. $\endgroup$
    – Bob Hanlon
    Mar 26 at 17:44
  • 2
    $\begingroup$ @Charlotte, Another possible workaround is Table[..., {i, Subdivide[a, b, n]}]. $\endgroup$
    – Michael E2
    Mar 26 at 17:59

1 Answer 1

7
$\begingroup$

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.311`30, 6.312`30, 0.001`30}]
 ]

(*
  {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.311`30, 6.312`30, 0.001`30]]}
 ]

(*
  {6.31100000000000000000000000000, 
   6.31200000000000000000000000000} 
*)

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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