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?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityYou 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).
a
has accuracy to 20 decimal places -- how many decimal places do you thinka^2
is accurate to? $\endgroup$Precision[a]
and you'll see how accuratea
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 givesb
to 3 decimal places. $\endgroup$a
to be $\sqrt2$ rounded to 20 digits of accuracy, and obtain the accurate result of squaring that rounded number. If so, you should doa = Round[Sqrt[2] 10^20]/10^20
, after whicha
anda^2
are exact rational numbers which you can compute to arbitrary accuracy. $\endgroup$