2
$\begingroup$

Why is N not giving the approximate result (removing the .)?

vectorj = Table[j, {j, 0, 0.1, 0.01}]
N[vectorj[[1]], 0]
(*out=0.*)

or

N[vectorj, 0]

or

NumberForm[vectorj[[1]], 0]

Rationalize does the job.

$\endgroup$

closed as off-topic by Szabolcs, andre314, Alexey Popkov, m_goldberg, MarcoB Apr 29 '17 at 2:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Szabolcs, andre314, Alexey Popkov, m_goldberg, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ Use Round instead. N[x,0] would give a result with zero precision (i.e. something useless). Look up Precision. $\endgroup$ – Szabolcs Apr 28 '17 at 16:35
  • $\begingroup$ What is the expected output of N[vectorj, 0]? $\endgroup$ – Kuba Apr 28 '17 at 16:46
  • $\begingroup$ @Kuba expected output is 0 rather than 0. $\endgroup$ – Andrea G Apr 28 '17 at 16:47
  • $\begingroup$ I know that, what about the rest of the list? $\endgroup$ – Kuba Apr 28 '17 at 18:06
3
$\begingroup$

It's the expected result from N. The result of N is an approximate result but not an exact result.

In:

vectorj = Table[j, {j, 0, 0.1, 0.01}]

N[vectorj[[1]], 0] // Head
0 // Head

N[vectorj[[1]], 0] // Accuracy 
0 // Accuracy

Out:

{0., 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1}
Real
Integer
307.653
\[Infinity]

Workaround:

In:

xs = Table[x/100, {x, 0, 10, 1}]
xs // N

Out:

{0, 1/100, 1/50, 3/100, 1/25, 1/20, 3/50, 7/100, 2/25, 9/100, 1/10}
{0., 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1}
$\endgroup$

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