I have two fractions which, when evaluated, may or may not be non-terminating (they go on forever). They're both between 1 and 0.
I need to find the shortest decimal number that lies between these fractions which terminates.
Anyone have an idea? I'm a simple programmer not a mathematician so try not make it too complicated if possible.
Thanks!
Let the two numbers be $\dfrac{a}b$ and $\dfrac{c}d$. Find the difference, i.e., $$\dfrac{p}q = \dfrac{\vert ad-bc\vert}{bd}$$ Find $m=\lfloor \log(q/p) \rfloor$. This gives the digit where the difference occurs. Now take the largest of the fraction. Find the first $m$ digits, this gives you the answer.
Example:
For instance, consider the fractions, $a/b=37/87$ and $c/d=195/459$. It is easy to check which is lesser. Compare $459 \times 37$ and $87 \times 195$. In this case, $195/459$ is lesser than $37/87$. Compute $$\dfrac{p}q = \dfrac{a}b - \dfrac{c}d = \dfrac2{4437}$$ Now we get $m = \lfloor \log_{10}(4437/2) \rfloor = 3$. Now compute $a/b$ till the third digit. We get $a/b \approx 0.4252\ldots$. Stop at the third digit, which gives us $0.425$. We have $$0.4248 \approx \dfrac{195}{459} < 0.425 \leq \dfrac{37}{87} \approx 0.4252$$