Algorithm to approximate decimal expansion for fraction

183 Views Asked by At

Let's say I have some fraction $\frac{n}{m}$, which is fully reduced. how can I approximate its decimal expansion to a given accuracy?

Like $\frac{1}{7}$ is 0.143 if you want 3 decimal places of accuracy but 0.14285714 if you want 8 decimal places of accuracy.

Currently I use the following algorithm

Let be $a \in \{1,2,\ldots\}$ a specifier for accuracy.

Calculate: $$ \begin{align} p &= \lceil \log_{10}(m) \rceil + a \\\\ f &= \lfloor \frac{10^p}{m} \rfloor \\\\ v &= n \cdot f \end{align} $$ Then in $v$ insert the decimal comma at the correct place or add 0. with leading zeros.

Is this a good algorithm or are there improvements I could do?

1

There are 1 best solutions below

0
On

You could do long division, see https://www.mathsisfun.com/long_division3.html for details.