A) For simple example suppose i want to approximate $\pi$ using $\frac{22}{7}$ and $\frac{355}{113}$.
In[120]:=
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):
- Suppose the exact value of $X$ is
123456789.987654321
. - Method $A$ gives
123456889.981236432
. - 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]