# HP Forums

Full Version: Ln(x) using repeated square root extraction
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Algorithm:

1. Enter x;
2. Take the square root 6 times;
3. Multiply by 2;
4. Subtract 1;
5. Take the square root;
6. Subtract 1;
7. Multiply by 64.

For x = 2, on an 8-digit calculator like the Canon LC-37, you should get ln(2) ~ 0.693152. For 10-digit calculators, in step 2 take the square root 9 times and multiply by 512 in the last step. Thus, on the HP-12C, which has two guard digits, we get ln(2) ~ 0.693147648. Ordinary 10-digit calculator might give less approximate results.

A 4-step less accurate algorithm is available in this old thread:

http://www.hpmuseum.org/cgi-sys/cgiwrap/...ead=145192

The accuracy range should be tested for your particular calculator. Use these at your own risk.

Gerson.

Method:

$\ln (x)=\int_{1}^{x}\frac{du}{u}=\int_{1}^{x}u^{-1}du=\lim_{v\rightarrow 0}\left | \frac{u^{v}}{v} \right |_{1}^{x}=\lim_{v\rightarrow 0} \left ( \frac{x^{v}}{v}-\frac{1^{v}}{v} \right )=\lim_{v\rightarrow 0} \left ( \frac{x^{v}-1}{v} \right )$

By testing the limit with a few values for v close to zero, an empirical second term has been able to be added to the expression:

$\ln (x)=\lim_{v\rightarrow 0} \left ( \frac{x^{v}-1}{v}-\frac{v\cdot \ln^{2}(x)}{2} \right )$

After turning the limit into an equality and solving the resulting quadratic equation for ln(x), we get

$\ln (x)=\lim_{v\rightarrow 0} \left ( \frac{\sqrt{2\cdot x^{v}-1}-1}{v} \right )$

Example:

Let x = 2 and v = 0.001

Then

$\ln (2)\approx \frac{\sqrt{2\cdot 2^{0.001}-1}-1}{0.001}\approx 0.69314723$

which is good to 6 decimal places ( ln(2) = 0.69314718 ).

P.S.: Perhaps 8 square root extractions and final multiplication by 256 (2^8) is a better compromise for the range 2..100 on the Canon LC-37. Contrary to what I thought its SILVA-CELL 189 battery is still working after all these years, so I was able to make some more tests. :-)
For the sake of completeness here is the recursive limit definition of ln(x), even though this is not quite necessary for our purpose:

$\ln (x)=\lim_{n\rightarrow \infty} \left [ n\cdot \left ( x^{\frac{1}{n}}-1 \right )-\sum_{k=2}^{\infty }\frac{\ln ^{k}(x)}{k!\cdot n^{k-1}} \right ]$

If the limit is removed and n is set to 1 then we'll have the following recursive series representation:

$\ln (x)= \ x-1 -\sum_{k=2}^{\infty }\frac{\ln ^{k}(x)}{k!}$
I've got a feeling I tried something along these lines for the LN function of the 34S. The LN function has always been very slow on the 34S and I've tried several implementations to try to get both performance and accuracy. The current implementation is much faster than the original but still far slower than I'd like. I ended up with:

Code:
/* Natural logarithm.
*
* Take advantage of the fact that we store our numbers in the form: m * 10^e
* so log(m * 10^e) = log(m) + e * log(10)
* do this so that m is always in the range 0.1 <= m < 2.  However if the number
* is already in the range 0.5 .. 1.5, this step is skipped.
*
* Then use the fact that ln(x^2) = 2 * ln(x) to range reduce the mantissa
* into 1/sqrt(2) <= m < 2.
*
* Finally, apply the series expansion:
*   ln(x) = 2(a+a^3/3+a^5/5+...) where a=(x-1)/(x+1)
* which converges quickly for an argument near unity.
*/

- Pauli
Reference URL's
• HP Forums: https://www.hpmuseum.org/forum/index.php
• :