3

Why the following code

Table[ N[i, 4], {i, 1, 2, 0.5}]

do not output numbers with fixed number of digits, but gives this {1., 1.5, 2.}

while there is no problems with this code

Table[ N[i, 4], {i, 1, 2}]

the output as expected is {1.000, 2.000}

1 Answers1

8

The presence of 0.5 in the iterator makes Table use machine-precision numbers for i, which apparently N will not modify the precision on (even if the requested precision is less than MachinePrecision).

To generate the list with the precision you want:

Table[ N[i,4], {i, 1,2, 1/2}]
eyorble
  • 9,383
  • 1
  • 23
  • 37
  • Is there a way to tell Mathematica to consider a decimal as arbitrary precision? – MTCoster Dec 30 '17 at 17:39
  • 3
    Rationalize[x,0] will return the closest rational number Mathematica can find, which will be arbitrary precision and usually fairly close (it's impossible to rationalize irrational or transcendental numbers, after all). However, it's not possible to directly say that some decimal number is infinitely precise. If you merely want an arbitrary finite precision, copy and paste any non-machine precision number and note the backtick followed by a number -- that's the listed precision of the number. – eyorble Dec 30 '17 at 17:45