0

I am interested in approximating the natural logarithm for implementation in an embedded system. I am aware of the Maclaurin series, but it has the issue of only covering numbers in the range (0; 2).

For my application, however, I need to be able to calculate relatively precise results for numbers in the range (0; 100]. Is there a more efficient way of doing so than decomposing each number greater than 2 into a product of factors in the (0; 2) range and summing up the results of the Maclaurin series for each factor?

4 Answers4

3

That's basically how a computer might do it. We have $$ \ln (a\cdot 2^b) = \ln(a)+ b\cdot\ln(2) $$ So $\ln(2)$ is just a constant that the computer can remember, and $b$ is an integer, and $a$ is set to be between $0.5$ and $1$ in most standard floating point formats.

Arthur
  • 204,511
1

Here's an algorithm for calculating base $2$ logarithms to whatever floating-point precision you're using. It takes one squaring and some bit operations per bit of precision. To get the natural logarithm, we then multiply by the constant $\ln 2$.

In modern systems, basic functions like logarithms, trig functions, and exponentials are implemented at a pretty low level. I strongly recommend looking into what's already there in the chips and languages you're using. Don't reinvent the wheel if you don't have to.

jmerry
  • 19,943
1

For the natural logarithm I recently wrote an algorithm for this myself. In my implementation, I found that the fastest method is to use the following iterative method. First take $x_0$ as an initial approximation to the logarithm, then use $$x_n=x_{n-1}-\frac{2(e^{x_{n-1}}-k)}{e^{x_{n-1}}+k}$$ Where we are attempting to find $\ln{(k)}$. For example, to find $\ln{(345.67)}$: $$x_0=6$$ $$x_1=6-\frac{2(e^6-345.67)}{e^6+345.67}=5.845791252...$$ $$x_2=5.845484563...$$ $$\ln{(345.67)}=5.845484563...$$

Peter Foreman
  • 20,095
0

In theory, a series convergent for all positive arguments exists::

$\ln(x)=2\sum_{m=1}^\infty {\dfrac{(\frac{x-1}{x+1})^{2m-1}}{2m-1}}$

In practice, Arthur's approach of halving the argument until we get to the Taylor series convergent range (argument $<2$) and adding back the appropriate number of $\ln 2$ terms seems more likely to gain use.

We may also reduce the argument per Arthur's method and then use the series above, which converges faster than the unmodified Taylor series for arguments between $1$ and $2$.

Oscar Lanzi
  • 48,208