# Number of digits problem

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}

• As @eyorble has pointed out, the presision of 0.5 is machine-precision. You could controll this by doing Table[ N[i, 4], {i, 1, 2, SetPrecision[ 0.5, 4 ] } ] where you specify the precision to be used for 0.5.
– gwr
Commented Dec 30, 2017 at 16:51
• ....or {i, 1, 2, 1/2} Commented Dec 30, 2017 at 16:57
• Helpful in this regard: How to Control the Precision and Accuracy of Numerical Results.
– gwr
Commented Dec 30, 2017 at 17:02

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).
Table[ N[i,4], {i, 1,2, 1/2}]

• 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. Commented Dec 30, 2017 at 17:45