Post Reply 
RPN83P: RPN calculator for TI-83+ TI-84+ inspired by HP-42S
11-24-2023, 07:54 PM
Post: #28
RE: RPN83P: RPN calculator for TI-83+ TI-84+ inspired by HP-42S
(11-22-2023 06:11 PM)bxparks Wrote:  But since I did not understand the explanation given in the math.stackexchange.com link about *why* it worked, I could not be sure that it worked for *all* ranges of numbers.

We can start with the geometric series for \(|q| < 1\):

\[
\begin{align}
\frac{1}{1-q}
&= \sum_{k=0}^\infty q^k \\
&= 1 + q + q^2 + q^3 + q^4 + q^5 + \cdots
\end{align}
\]

Substitute \(q = -x\) to get:

\[
\begin{align}
\frac{1}{1+x}
&= 1 - x + x^2 - x^3 + x^4 - x^5 + \cdots \\
\end{align}
\]

Integrate both sides:

\[
\begin{align}
\int \frac{1}{1+x} \; \mathrm{d}x
&= \log(1 + x) \\
&= x - \frac{x^2}{2} + \frac{x^3}{3} - \frac{x^4}{4} + \frac{x^5}{5} - \frac{x^6}{6} + \cdots \\
\end{align}
\]

In the following I'm using the same definitions of \(x\), \(y\) and \(z\) as in your code.

Example

I assume we use a 10-digit calculator.

x = 1.234509876e-5
y = 1 + x
    1.000012345
z = y - 1
    0.000012345

Due to cancellation we can't calculate \(\log(1+x)\) but calculate \(\log(1 + z)\) instead.

Thus we get:

\[
\begin{align}
\log(1 + z)
&= z - \frac{z^2}{2} + \frac{z^3}{3} - \frac{z^4}{4} + \frac{z^5}{5} - \frac{z^6}{6} + \cdots \\
&= z \cdot \left(1 - \frac{z}{2} + \frac{z^2}{3} - \frac{z^3}{4} + \frac{z^4}{5} - \frac{z^5}{6} + \cdots \right) \\
\end{align}
\]

We can improve the result by first dividing by \(z\) and then multiplying by \(x\):

\[
\begin{align}
\frac{x \cdot \log(1 + z)}{z}
&= x \cdot \left(1 - \frac{z}{2} + \frac{z^2}{3} - \frac{z^3}{4} + \frac{z^4}{5} - \frac{z^5}{6} + \cdots \right) \\
&= x - \frac{xz}{2} + \frac{xz^2}{3} - \frac{xz^3}{4} + \frac{xz^4}{5} - \frac{xz^5}{6} + \cdots \\
\end{align}
\]

Now both series start with \(x - \cdots\) which is the dominant term if \(x\) is small.

If \(x\) is very close to \(0\), that is \(z = 0\), we can ignore the higher order terms and just return \(x\).

If \(|x| > 1\), then the cancellation is minuscule and thus \(x \approx z\).
Therefore the factor \(\left | \frac{x}{z} \right | \approx 1\) and the multiplication doesn't change much.

I could be wrong, but I don't see a case where the use of this trick would lead to a problem.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: RPN83P: RPN calculator for TI-83+ TI-84+ inspired by HP-42S - Thomas Klemm - 11-24-2023 07:54 PM



User(s) browsing this thread: 6 Guest(s)