Skip to content

Commit

Permalink
Removed break tags around approximations.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachartrand committed Mar 28, 2024
1 parent d189b4b commit da911c3
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Living version created on 17 Oct 2023
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script>
</head>

*Note: This is an updated version of the SoME 3 entry. See the original
*Note: This is an updated version of the SoME 3 entry. See the original
[here](https://zachartrand.github.io/SoME-3/).*

# How does a computer/calculator compute logarithms?
Expand Down Expand Up @@ -178,7 +178,7 @@ We'll start by setting the natural logarithm equal to the integral of our geomet
$$
\begin{align*}
\ln(1+x) & = \int_{0}^{x} \frac{1}{1+t} dt \\
& = \int_{0}^{x} (1 - t + t^2 - t^3 + t^4 + \ldots + (-t)^n + \ldots) \space dt
& = \int_{0}^{x} (1 - t + t^2 - t^3 + t^4 + \ldots + (-t)^n + \ldots) \space dt
\end{align*}
$$

Expand Down Expand Up @@ -313,7 +313,7 @@ each term gets smaller and smaller in magnitude. In this particular example, th
less than one tenth the absolute value of the previous term. This means that the approximation gains
about one digit of accuracy for each term we add to the series. To see this, here is a list of the
first eight approximations for $\ln(15)$, as well as the accepted value:
<br>

$$
\begin{align*}
& \text{0 terms:} && 2.772588722239781 \\
Expand All @@ -329,9 +329,9 @@ $$
& \text{Accepted value:} && 2.708050201102210
\end{align*}
$$
<br>

So now we have a formula for finding the natural logarithm of any number to arbitrary precision
based on the number of terms we use in the series. We did it! I even have a
based on the number of terms we use in the series. We did it! I even have a
[Python script](https://github.com/zachartrand/SoME-3-Living/blob/main/scripts/log.py)
that implements this formula to return the natural logarithm of any value within 64-bit floating-point
precision using 48 terms.
Expand Down Expand Up @@ -364,7 +364,7 @@ what would the series be for $\ln(1-x)$? We can rewrite $\ln(1-x)$ as $\ln(1 + (
into our natural log series:

$$
\begin{align}
\begin{align}
\ln(1-x) & = \ln(1 + (-x)) \nonumber \\
& = (-x) - \frac{(-x)^2}{2} + \frac{(-x)^3}{3} - \frac{(-x)^4}{4} - \ldots \nonumber \\
& = -x - \frac{x^2}{2} - \frac{x^3}{3} - \frac{x^4}{4} + \ldots \nonumber \\
Expand Down Expand Up @@ -402,14 +402,14 @@ value of $x$ to plug into our series that will get us the natural log of $u$. We
setting $u$ equal to the input of each new, combined logarithm and solve for $x$. For the added
logarithm, set $u$ equal to $1 - x^2$:

[^artanh]: The function $\ln \left( \frac{1+x}{1-x} \right)$ happens to be the inverse hyperbolic tangent function $ \big( \text{artanh}(x)$ or $\tanh^{-1}(x) \big)$
[^artanh]: The function $\ln \left( \frac{1+x}{1-x} \right)$ happens to be the inverse hyperbolic tangent function $ \big( \text{artanh}(x)$ or $\tanh^{-1}(x) \big)$
multiplied by 2.

$$
\begin{gather*}
u = 1 - x^2 \\
x^2 = 1 - u \\
x = \sqrt{1-u}
x^2 = 1 - u \\
x = \sqrt{1-u}
\end{gather*}
$$

Expand All @@ -429,17 +429,17 @@ $$

The addition of the logarithms requires taking a square root—a complicated function in and of itself—to convert
our main input into the series input, while the subtraction of the logarithms only requires the basic operations of
addition, subtraction, and division.[^radiusofconvergence] Subtracting the logarithms is the clear winner.
addition, subtraction, and division.[^radiusofconvergence] Subtracting the logarithms is the clear winner.
Now we can take our two series and combine them in this manner:

[^radiusofconvergence]: This also conveniently maps all of the positive reals to the domain $x \in (-1, 1)$, giving
us an infinite radius of convergence, as we see in the graph.

$$
\begin{align*}
\ln(1+x) - \ln(1-x) = \space & x - \frac{x^2}{2} + \frac{x^3}{3} - \frac{x^4}{4}
\ln(1+x) - \ln(1-x) = \space & x - \frac{x^2}{2} + \frac{x^3}{3} - \frac{x^4}{4}
+ \frac{x^5}{5} - \frac{x^6}{6} + \ldots \\
- \space (- & x - \frac{x^2}{2} - \frac{x^3}{3} - \frac{x^4}{4}
- \space (- & x - \frac{x^2}{2} - \frac{x^3}{3} - \frac{x^4}{4}
- \frac{x^5}{5} - \frac{x^6}{6} - \ldots)
\end{align*}
$$
Expand All @@ -448,7 +448,7 @@ Distributing the negative sign on the second series, we get

$$
\begin{align*}
\ln(1+x) - \ln(1-x) = \space & x - \frac{x^2}{2} + \frac{x^3}{3} - \frac{x^4}{4} + \frac{x^5}{5}
\ln(1+x) - \ln(1-x) = \space & x - \frac{x^2}{2} + \frac{x^3}{3} - \frac{x^4}{4} + \frac{x^5}{5}
- \frac{x^6}{6} + \ldots \\
+ \space & x + \frac{x^2}{2} + \frac{x^3}{3} + \frac{x^4}{4} + \frac{x^5}{5} + \frac{x^6}{6} + \ldots ,
\end{align*}
Expand All @@ -468,23 +468,23 @@ Substituting our initial value, $u$, into the input, we get
$$
\begin{align}
\ln(u) & = \sum_{n=0}^{\infty} \frac{2}{2n+1} {\biggl( \frac{u-1}{u+1} \biggr)}^{2n+1} \\
& = 2{\biggl( \frac{u-1}{u+1} \biggr)} + \frac{2}{3} {\biggl( \frac{u-1}{u+1} \biggr)}^3
+ \frac{2}{5} {\biggl( \frac{u-1}{u+1} \biggr)}^5 + \frac{2}{7} {\biggl( \frac{u-1}{u+1} \biggr)}^7 + \ldots
& = 2{\biggl( \frac{u-1}{u+1} \biggr)} + \frac{2}{3} {\biggl( \frac{u-1}{u+1} \biggr)}^3
+ \frac{2}{5} {\biggl( \frac{u-1}{u+1} \biggr)}^5 + \frac{2}{7} {\biggl( \frac{u-1}{u+1} \biggr)}^7 + \ldots
\end{align}
$$

We now have a series that increases by two degrees for each new term added! But how much quicker does
this series converge compared to our old series? I have made a
[Desmos graph](https://www.desmos.com/calculator/zr1vhmoign) where you can compare the two series.
The first series for $\ln(1+x)$ (shifted right by 1) is in red and our new series is in green.
Slide the value for $N$ to add terms to the series approximations, and note how they converge
both by looking at the graph and by looking at the calculation for the natural log of 2.
The first series for $\ln(1+x)$ (shifted right by 1) is in red and our new series is in green.
Slide the value for $N$ to add terms to the series approximations, and note how they converge
both by looking at the graph and by looking at the calculation for the natural log of 2.
(Also, notice the difference in the radius of convergence.) The old series takes 36 terms to reach Desmos' calculator
accuracy (11 decimal places), but the new series only takes 12! We reduced the number of terms we need
by one third! And when you have the option to reduce the argument like we did with the example to find
the natural log of 15, the number of terms reduces further. In my
[Python script for the new series](https://github.com/zachartrand/SoME-3-Living/blob/main/scripts/log_fast.py),
the code calculates the natural logarithm for any number with 15 terms,
the code calculates the natural logarithm for any number with 15 terms,
less than one third of the 48 terms needed for my script for the other series!
This series is what is used in the
[logarithm function used in the C library](https://github.com/freemint/fdlibm/blob/master/e_log.c),
Expand All @@ -497,7 +497,7 @@ reduction algorithm to get an answer accurate to double floating-point precision

*Pictured: A comparison of the 3-term approximations for the series*
*$\sum_{n=0}^{\infty} (-1)^n \frac{(x-1)^{n+1}}{n+1}$ (red) and*
*$\sum_{n=0}^{\infty} \frac{2}{2n+1} {\biggl( \frac{x-1}{x+1} \biggr)}^{2n+1}$*
*$\sum_{n=0}^{\infty} \frac{2}{2n+1} {\biggl( \frac{x-1}{x+1} \biggr)}^{2n+1}$*
*(green) to $\ln(x)$ (dotted black). Even after only three terms, the latter series*
*is a much better approximation than the former.*

Expand All @@ -517,8 +517,8 @@ I learned about different techniques of coding mathematical formulae that avoide
would either create an inaccurate result or throw an error despite a calculable value existing.
However, when it came to the basic complicated math functions, like the logarithm, trigonometric, and
exponential functions, I found that the Python source code didn't have them written up, because Python
used the functions from the C library. This led me to a new adventure where I looked into
[the original C code](https://github.com/freemint/fdlibm), which, while more difficult to read, is
used the functions from the C library. This led me to a new adventure where I looked into
[the original C code](https://github.com/freemint/fdlibm), which, while more difficult to read, is
surprisingly readable once you have learned Python.
The trig functions worked more or less how I expected, but I was surprised that the natural logarithm
used a different series than the one I had learned about in my calculus class. (So does the exponential
Expand All @@ -536,7 +536,7 @@ exercise, now I know, and if you made it this far, so do you! Thank you for join
math adventure. I hope you found this exercise as interesting and enlightening as I have.

### Support
Did you enjoy this article? Want to help me make more? Consider making a
Did you enjoy this article? Want to help me make more? Consider making a
[donation](https://www.buymeacoffee.com/zachartrand).

### Footnotes

0 comments on commit da911c3

Please sign in to comment.