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
    $\begingroup$ This is a fairly good algorithm, but you may want to change the 10 to 2... That is, $p = \lceil \log_2{(m)} \rceil + a$, and $f = \lfloor \frac{2^p}{m} \rfloor$. According to en.wikipedia.org/wiki/… the Newton-Raphson division will divide numbers in the time it takes to multiply, so this would be better for larger numbers. Also, you could try asking this question on Computer Science Stack Exchange or CSTheory Stack Exchange. $\endgroup$
    – Matt Groff
    Commented Apr 24, 2020 at 9:59
  • $\begingroup$ @MattGroff I dont see why choosing $2$ as a base would improve it? Because what I'm basicly doing is searching for $f$ so that $\min(\left| 10^p - f \cdot m\right| )$ is close to $0$. So in words I search for a factor that brings $m$ the closest to some power of $10$. $\endgroup$
    – Ackdari
    Commented Apr 24, 2020 at 10:10
  • $\begingroup$ I believe that in practice, $2^p$ is used so that the computer can do a simple bit shift, instead of actually taking $2$ to the $p$th power. That's why I said that. $\endgroup$
    – Matt Groff
    Commented Apr 24, 2020 at 10:13
  • 1
    $\begingroup$ See also cs.stackexchange.com/q/124673/755. $\endgroup$
    – D.W.
    Commented Apr 24, 2020 at 18:06
  • $\begingroup$ @MattGroff, May I make a request? If you suggest another site, it would be nice if you could let the poster know to not cross-post. Hopefully this will provide a better experience for all. Thanks for listening! P.S. This doesn't seem to be research-level and thus might not be well-received on CSTheory. $\endgroup$
    – D.W.
    Commented Apr 24, 2020 at 18:11

1 Answer 1


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


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .