2
$\begingroup$

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}

$\endgroup$
  • 3
    $\begingroup$ 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. $\endgroup$ – gwr Dec 30 '17 at 16:51
  • 1
    $\begingroup$ ....or {i, 1, 2, 1/2} $\endgroup$ – David G. Stork Dec 30 '17 at 16:57
  • $\begingroup$ Helpful in this regard: How to Control the Precision and Accuracy of Numerical Results. $\endgroup$ – gwr Dec 30 '17 at 17:02
7
$\begingroup$

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}]
$\endgroup$
  • $\begingroup$ Is there a way to tell Mathematica to consider a decimal as arbitrary precision? $\endgroup$ – MTCoster Dec 30 '17 at 17:39
  • 2
    $\begingroup$ 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. $\endgroup$ – eyorble Dec 30 '17 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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