Alright, even though intuitively this looks like something that should work, it doesn't. This is because of the way Mathematica handles precision. You might have noticed that you can output really large numbers like 10^10000 which are way beyond MachinePrecision
of double
. This is because Mathematica is a symbolic language. Each value gets it's precision which you can look at by calling Precision[a]
Symbols, Integers and some other stuff has $\infty$ precision and some things have MachinePrecision
. It is written in the documentation that
N[e] typically works by replacing numbers with machine numbers and computing the result.
and that:
The precision of the result is the same as that of the input:
This means if you have machine precision it will N
will always return the same precision. In your sanity check if you wrote
N[5.,10]
instead of N[5,10]
you would get 5.
Now you can change the precision by using SetPrecision
as I did in the comment and things will go smoother.
EDIT
Also N
Will try to give you the result that can be Exactly computed without machine errors. So there's also that.