3
$\begingroup$

Please look at this computations:

a = N[Sqrt[2], 20];
N[a^2, 100]

The output is 2.0000000000000000000, which is apparently not $a^2$ to 100 decimal places.

What's wrong?

$\endgroup$
  • 1
    $\begingroup$ a has accuracy to 20 decimal places -- how many decimal places do you think a^2 is accurate to? $\endgroup$ – bill s Feb 18 '14 at 18:53
  • $\begingroup$ @bills Does it matter? I thought N[a^2, 100] means that 'calculate $a^2$ and show up to 100 decimal places. $\endgroup$ – Behzad Feb 18 '14 at 18:55
  • 3
    $\begingroup$ Try Precision[a] and you'll see how accurate a is... Precision[a^2] shows how accurate a^2 is. You can't expect any more accuracy in a derived calculation than in the inputs. For a simpler example: b = 2.343; N[b, 100] only gives b to 3 decimal places. $\endgroup$ – bill s Feb 18 '14 at 18:59
  • $\begingroup$ @bills I expected Mathematica to give the right answer to a simple computation such as 1.999999^2; which is 3.99999680000064. I don't want to write 1.999999^2 as 1.9999990000000^2 to get to the answer! How can I make Mathematica to do so? $\endgroup$ – Behzad Feb 18 '14 at 19:08
  • 1
    $\begingroup$ Perhaps you want a to be $\sqrt2$ rounded to 20 digits of accuracy, and obtain the accurate result of squaring that rounded number. If so, you should do a = Round[Sqrt[2] 10^20]/10^20, after which a and a^2 are exact rational numbers which you can compute to arbitrary accuracy. $\endgroup$ – user484 Feb 18 '14 at 19:19
11
$\begingroup$

You can see how accurate a number is represented using the Accuracy[] function. For the OPs example:

a = N[Sqrt[2], 20];

{Accuracy[a], Accuracy[a^2], Accuracy[N[a^2,100]]}
{19.8495, 19.3979, 19.3979}

Hence, when printing out N[a^2,100], only the first 20 (or so) digits are significant and are printed. This is also true in general, for example:

b = 2.343; b*a
3.3135

The product is returned only with the accuracy of the least accurate of its two components. If you want greater accuracy, you can specify more digits (as you suggest in your comment) or you can keep things infinitely accurate by using rationals. Compare:

{1.999999^2, 1.999999900000000000^2, (1999999/1000000)^2}
{4., 3.99999960000001000, 3999996000001/1000000000000}

The first two are both approximate (and calculated to the accuracy of the inputs), while the third is exact. If you want to work with reals, then you can set the accuracy to a specified number. For example, with b=1.999999

c = SetAccuracy[b, 100]

then both c and c^2 are calculated to 100 decimal places. There is a shortcut for this function, which uses the double back-tick marks:

c = 1.999999``100

gives c to 100 decimal places of accuracy.

So I guess the short answer is that SetAccuracy (or the double back-ticks) do what you thought N would do (but doesn't).

| improve this answer | |
$\endgroup$

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