Ln(x) using repeated square root extraction - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: Not HP Calculators (/forum-7.html) +--- Forum: Not remotely HP Calculators (/forum-9.html) +--- Thread: Ln(x) using repeated square root extraction (/thread-5907.html) Ln(x) using repeated square root extraction - Gerson W. Barbosa - 03-21-2016 12:03 AM 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/hpmuseum/archv018.cgi?read=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. :-) ----------------------------------- Good Friday 2020 Update The following expression allows us to go further: $\ln (x)=\lim_{v\rightarrow 0} \left ( \frac{x^{v}-1}{v}-\frac{v\cdot \ln^{2}(x)}{2!} -\frac{v^{2}\cdot \ln^{3}(x)}{3!} -\frac{v^{3}\cdot \ln^{4}(x)}{4!} - \cdots \right )$ By going up to the third power of ln(x) and solving the corrisponding cubic equation, we get $\ln (x)=\lim_{v\rightarrow 0}\frac{1}{v}\left ( \sqrt[3]{\sqrt{9x^{2v}-6x^{v}+2}+3x^{v}-1} - \frac{1}{\sqrt[3]{\sqrt{9x^{2v}-6x^{v}+2}+3x^{v}-1}} - 1 \right )$ For example, if x = 2 and v = 0.01 then ln(2) ~ 0.69314719 This approximation is rather complicated for manual calculations. Also, it requires one cubic root extraction. It can be used, however, as an alternative method for implementing the ln(x) function to 7 significant digits on calculators that lack it, like the HP-16C. The e^x function has also been implemented. Since it's based on the first formula, obtained by solving a quadratic equation, and uses a constant number of loops, it gives less significant digits. The cubic root algorithm is optimized for the narrow range required by the approximation formula and was based one provided by Albert Chan here. Code:  001- LBL A; LN 002- 1 003- EEX 004- CHS 005- 2 006- CF 1 007- 1 008- + 009- STO I 010- CLx 011- LASTx 012- x⇆y 013- x≤y 014- GTO 2 015- LBL 0 016- √x 017- RCL I 018- x>y 019- GSB 1 020- R↓ 021- x⇆y 022- ENTER  023- + 024- x⇆y 025- GTO 0 026- RTN 027- LBL 1 028- R↓ 029- STO 0 030- R↓ 031- 2 032- × 033- STO 1 034- RCL 0 035- ENTER  036- × 037- 9 038- × 039- 6 040- RCL 0 041- × 042- - 043- 2 044- + 045- √x 046- 3 047- RCL 0 048- × 049- + 050- 1 051- - 052- GSB 3 053- ENTER  054- 1/x 055- - 056- 1 057- - 058- RCL 1 059- × 060- F?1 061- CHS 062- RTN 063- LBL 2 064- SF1 065- 1/x 066- RTN  067- LBL 3 068- 2 069- STO I 070- ENTER  071- + 072- x⇆y 073- × 074- STO 0 075- LASTx 076- 2 077- - 078- 6 079- / 080- √x 081- 1 082- + 083- LBL 4 084- ENTER  085- ENTER  086- RCL 0 087- x⇆y 088- / 089- LASTx 090- ENTER  091- × 092- - 093- 3 094- / 095- √x 096- + 097- 2 098- / 099- DSZ 100- GTO 4 101- RTN 102- LBL B; eᵡ 103- 1 104- 3 105- STO I 106- R↓ 107- 8 108- 1 109- 9 110- 2 111- + 112- LASTx 113- / 114- ENTER 115- × 116- 1 117- + 118- 2 119- / 120- LBL 5 121- ENTER  122- × 123- DSZ 124- GTO 5 125- RTN Examples: 2 GSB A -> 0.6931471(360) GSB B -> 2.0000(13165) 12345 GSB A -> 9.421006(848) GSB B -> 12345.0(1957) 6.789 EEX 79 GSB A -> 183.8195(343) GSB B -> 6.(686887E79) 0.12345 GSB A -> -2.091919(104) GSB B -> 0.123450(119) 230 GSB B -> 7.496895E99 GSB A -> 229.97041(15) 1 GSB A -> 0.000000000 GSB B -> 1.000000000 GSB B -> 2.7182(92170) GSB B -> 15.1544(4181) GSB A -> 2.71829(4016) GSB A -> 1.000004(736) GSB A -> 0.000004736 0.693147181 CHS GSB B -> 0.500000(16) 10 GSB A -> 2.302585(344) STO 2 2 GSB A RCL 2 / -> 0.3010299(435) 0 GSB A -> Error 0 2 CHS GSB A -> Error 0 RE: Ln(x) using repeated square root extraction - Gerson W. Barbosa - 03-21-2016 05:09 AM 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!}$ RE: Ln(x) using repeated square root extraction - Paul Dale - 03-21-2016 07:11 AM 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