Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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?

share|improve this question

marked as duplicate by Mr.Wizard Feb 18 '14 at 22:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

a has accuracy to 20 decimal places -- how many decimal places do you think a^2 is accurate to? – bill s Feb 18 '14 at 18:53
@bills Does it matter? I thought N[a^2, 100] means that 'calculate $a^2$ and show up to 100 decimal places. – Behzad Feb 18 '14 at 18:55
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. – bill s Feb 18 '14 at 18:59
@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? – Behzad Feb 18 '14 at 19:08
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. – Rahul Feb 18 '14 at 19:19
up vote 11 down vote accepted

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

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).

share|improve this answer

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