A) For simple example suppose i want to approximate $\pi$ using $\frac{22}{7}$ and $\frac{355}{113}$.

N[Pi, 10]
N[22/7, 10]
N[355/113, 10]

Out[120]= 3.141592654

Out[121]= 3.142857143

Out[122]= 3.141592920

I need a function to find the number of correct digits. As we can see, $\frac{22}{7}$ has 3 correct digits (3,1, and 4) and $\frac{355}{133}$ has 7 correct digits (3,1,4,1,5,9, and 2). I've read some similar questions and the documentation about Accuracy and Precision, but i'm not sure if i can use them or not or conveniently speaking i don't know how.

B) For advance application, suppose i have 2 methods for solving problem $X$, namely $A$ and $B$. I want to compare those 2 methods with exact value. It's not a problem if the number of correct digits are different so that i can decide which methods is better. But what about if those two are resulting the same correct digit but different digit? (To understand what i'm trying to say, please consider this example below):

  1. Suppose the exact value of $X$ is 123456789.987654321.
  2. Method $A$ gives 123456889.981236432.
  3. Method $B$ gives 123456412.638281645.

Now we can see if $A$ and $B$, both have the same correct digits (6 digits) but Method $A$ is better than Method B since the next digit of $A$ is 8 (close to 7 and the difference is 1), while the next digit of $B$ is 4 (the difference is 3). Suppose randomly i say A has $6.899$ correct digit and $B$ has $6.622$ correct digits, since $A>B$ then $A$ is better than $B$. And in my case i also expect the higher of the number of correct digits give the better approximation.

In conlusion, i need to find the number of digits that match with the given problem $X$ and the result not always as an integer just in case i want to compare different problems that have the same correct digits.

Thanks in advance!

For experimenting the problem, you could use $X$ as N[Pi, 10], $A$ as N[22/7, 10], and $B$ as N[22/7, 10]


A) To approximate a number by a fraction

Convergents[Pi, 5]


This approach is based on the continued fraction representation of a number.

B) To find the number of correct digits in an approximation a to an exact number e one can use

CorrectDigits[a_, e_] := Module[{man, exp, p},
  p = 16;
  man = 0;
  While[man == 0, p++;
   {man, exp} = MantissaExponent[a - N[e, p]];

C) To approximate a number Pi by a continued fraction and find out the number of correct digits in the result one can use

CorrectDigits[Convergents[Pi, 1200] // Last, Pi]
(* 1206 *)

It means if one takes a continued fraction expansion of $\pi$ with 1200 terms, one correctly gets 1206 decimal digits of the number.

  • 1
    $\begingroup$ Thanks for the reply. But i'm afraid you missunderstood the question. I mean i need a "function"/ built in or whatever it's called, like whenever i input Function[A,X]=6.899 and Function[B,X]=6.622. Where "Function" is a defined command to find the correct digit. A and B are the approximation and X is the exact value. Both argument are needed to calculate the number of correct digits. $\endgroup$ – user516076 Mar 13 at 8:41
  • 2
    $\begingroup$ @user516076 Yes, probably I misunderstood. I re-read your post, and added a function CorrectDigits. Does it answer your question? $\endgroup$ – yarchik Mar 13 at 9:10
  • $\begingroup$ yes! Thank you so much. $\endgroup$ – user516076 Mar 13 at 11:08

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.