6
$\begingroup$

Let $a=\sqrt{3}$. What's the point of having

In[102]:= N[Sqrt[3],100]-N[Sqrt[3],10]
Out[102]= 0.*10^-10

?

Let's imagine I wanted to get the difference between the number with the exact 99 decimal places and the one with just 9. How could I do it?

Any help would be appreciated.

$\endgroup$
4
$\begingroup$
N[Sqrt[3], 100] - SetPrecision[N[Sqrt[3], 10], 100]
(*4.34434192962723237304139596993250525836360687959547866217794*10^-40*)
$\endgroup$
  • $\begingroup$ Thanks for the answer. Do you know what's the logic for N[ ] not doing this directly? $\endgroup$ – An old man in the sea. Aug 25 '16 at 19:41
  • 1
    $\begingroup$ @Anoldmaninthesea. Probably because they are equal out to 10 digits (since they are the same number), and it chooses the lower precision of the two numbers, as it should. $\endgroup$ – march Aug 25 '16 at 19:45
  • $\begingroup$ when you add numbers with different precision the result takes the precision of the lowest precision value. $\endgroup$ – george2079 Aug 25 '16 at 19:45
  • $\begingroup$ @Anoldmaninthesea. You shouldn't think of N[x,10] aus having all zeros after the 10th decimal. It just means it is exact to this place. So it just logical that the result must also have the same precision. $\endgroup$ – sebhofer Aug 25 '16 at 20:04
  • 3
    $\begingroup$ I hate to point this out, given the upvotes, but if the difference is on the order of 10^-40, that means that all the digits from the 10th decimal place to the 39th are zero. N is doing something a bit more complicated than you probably think. $\endgroup$ – Michael E2 Aug 26 '16 at 2:00
10
$\begingroup$

The problem with using N to generate arbitrary precision numbers is that is it will generate more digits of accuracy than asked for:

N[Sqrt[3], 10] // InputForm
(*  1.73205080756887729352744634150587236694`10.  *)

N[Sqrt[3], 28] // InputForm
(*  1.73205080756887729352744634150587236694`28.  *)

Probably the square root algorithm converges quadratically, so it's quite likely that it will generate quite a few extra digits of accuracy just by the discrete nature of the iterations. Why not keep the extra precision? But beyond that, arbitrary-precsion numbers always carry at least a few extra guard bits, so that rounding error is unlikely to undermine the precision tracking.

The upshot is that given the near unpredictability of the number of digits in the internal representation of arbitrary-precision numbers, it's virtually impossible to subtract off the first few digits as the OP desires by using N.

Instead one should use Round or Floor, depending on what is meant by "the exact 99 decimal places" and so forth.

Round[Sqrt[3], 1*^-99] - Round[Sqrt[3], 1*^-9]
% // N
(*
-(67362922886336509139707442665186679092128026866280159460635466107999\
99420607090831917991/
   1562500000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000)

-4.31123*10^-10
*)

Floor[Sqrt[3], 1*^-99] - Floor[Sqrt[3], 1*^-9]
% // N
(*
2275509174109785366023489467771221015241522512223227917807732067635200\
1483245847470289943/\
4000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000

5.68877*10^-10
*)

Update: Answer to the "What's the point?" question

Much of this is explained in the tutorial Numerical Precision and discussed further in my answer to Increasing the precision of a calculation. Basically a precision of $p$ in a number $x$ represents an (absolute) error or uncertainty of at most $10^{-p}x$; in other words, it represents a relative error of $10^{-p}$. When Mathematica displays a arbitrary-precision number of precision $p$, it displays only the digits which are certain, that is, $p$ of them (rounded).

When you add or subtract numbers the uncertainties are added and the precision changes. In the OP's example N[Sqrt[3],100]-N[Sqrt[3],10], we have two nearly equal numbers (two approximations to Sqrt[3]) being subtracted. The uncertainty will be about $(10^{-10}+10^{-100})\sqrt{3} \approx \sqrt{3}\times 10^{-10}$. In the OP's example, the two terms of the difference agree past the first ten digits, so the result is smaller than the uncertainty. When that happens Mathematica gives 0 as the answer with an accuracy that reflects the uncertainty:

N[Sqrt[3], 100] - N[Sqrt[3], 10] // InputForm
(*  0``9.761439372640167  *)

10^-Accuracy[%]
(*  1.73205*10^-10  *)

Note that the Accuracy[] reflects an uncertainty of around $\sqrt{3}\times 10^{-10}$ as claimed.

So the point, or at least a point, of arbitrary precision is to make tracking the propagation of error through a computation automatic, and to give users, not to mention built-in functions, the tools for doing so. (Just a little caveat: The calculation of precision is approximate, although quite accurate. There must be edge-cases where there are issues. I don't seem to recall any serious examples, though. One can use Interval[] for rigorous bounds, but computations with Interval[] sometimes give rather loose bounds.)

$\endgroup$

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.