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:
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. - Pauli RE: Ln(x) using repeated square root extraction - Thomas Klemm - 03-03-2022 11:20 PM Formula We can use the following identity: \(\log(x) = n \log\left(x^{\frac{1}{n}}\right) = n \log\left(\sqrt[n]{x}\right)\) For say \(n = 2^{10} = 1024\) and \(1 \leqslant x \leqslant 100 \) the value of \(\sqrt[n]{x}\) is close to \(1\). Thus we can use the Taylor series to calculate the logarithm: \(\log(1 + \varepsilon) = \varepsilon - \frac{\varepsilon^2}{2} + \frac{\varepsilon^3}{3} - \frac{\varepsilon^4}{4} + \frac{\varepsilon^5}{5} + \mathcal{O}(\varepsilon^6)\) Program Here's a program for the HP-42S that calculates both the logarithm and its approximation: Code: LN LASTX It's easy to extend if you want to use more terms. Example x = 2 0.69314718056 0.69314718056 Comparison We can compare this to your solution: \(\sqrt{2(1 + \varepsilon) - 1} - 1\) The Taylor series agrees for the first two terms: \(\varepsilon - \frac{\varepsilon^2}{2} + \frac{\varepsilon^3}{2} - \frac{5 \varepsilon^4}{8} + \frac{7 \varepsilon^5}{8} + \mathcal{O}(\varepsilon^6)\) RE: Ln(x) using repeated square root extraction - Dan C - 03-05-2022 05:32 PM I just tried this on my CASIO fx-4000P, and the result was 0.693147648 I did the square root 9 times, and multiplied with 512 in the last step. RE: Ln(x) using repeated square root extraction - Dan C - 03-05-2022 05:53 PM (03-05-2022 05:32 PM)Dan C Wrote: I just tried this on my CASIO fx-4000P, and the result was 0.693147648 I just had to try this on my CASIO fx-4500P also, and the result was the same, 0.693147648. Now i just have to try this on my FX-602P also RE: Ln(x) using repeated square root extraction - Dan C - 03-05-2022 08:02 PM On my FX-602P this gives 0.693147371 hmm, different from my fx-4000p and fx-4500p. What does this tell of the FX-602P? Less accurate? RE: Ln(x) using repeated square root extraction - Namir - 03-05-2022 08:29 PM The extended limit for ln(x) does very well with values of v = 0.001 or less. Very impressive! Namir RE: Ln(x) using repeated square root extraction - Thomas Klemm - 03-05-2022 08:39 PM We experience cancellation when calculating \(\varepsilon = \sqrt[n]{x} - 1\). The program was executed on free42 with 34 decimal digits of precision. Thus this effect is not noticed. It is a trade-off between coming close to \(1\) and how many terms to use in the Taylor series. For a calculator like the HP-15C with 10 digits using \(n = 2^6\) might be the best choice. For \(x = 2\) we end up with the following sequence of iterated square roots: 2.000000000 1.414213562 1.189207115 1.090507733 1.044273783 1.021897149 1.010889286 This leaves us with \(\varepsilon = 0.010889286\) and thus 5 terms of the Taylor series should be enough. We notice the cancellation since we have now only 8 significant digits left. Here's the program for the HP-15C or similar calculators: Code: 001 { 11 } √x̅ For \(x = 2\) we get: 0.6931471776 While for \(\ln(2)\) we get: 0.6931471806 This leaves us with a difference of: 0.0000000030 RE: Ln(x) using repeated square root extraction - Albert Chan - 03-06-2022 12:04 AM (03-03-2022 11:20 PM)Thomas Klemm Wrote: We can compare this to your solution: log1p(ε) = 2*atanh(y) = 2*(y+y^3/3+y^5/5+...) ≈ 2y, where y=ε/(2+ε) 2ε/(2+ε) is simpler than √(1+2ε)-1, and twice as accurate, when ε is tiny. CAS> series(2*ε/(2+ε),ε) // estimate for log1p(ε) ε - 1/2*ε^2 + 1/4*ε^3 - 1/8*ε^4 + 1/16*ε^5 + ε^6*order_size(ε) CAS> (1/3-1/2)/(1/3-1/4) -2. Is is funny sqrt approximation formula give better estimate for log1p, than sqrt itself. CAS> pade(√(1+2*x)-1,x,2,2) 2*x/(x+2) RE: Ln(x) using repeated square root extraction - Albert Chan - 03-06-2022 12:50 AM (03-05-2022 08:39 PM)Thomas Klemm Wrote: For \(x = 2\) we end up with the following sequence of iterated square roots: It is more work, but we could improve accuracy by "pulling out" 1. CAS> f(x) := x/(sqrt(1+x)+1) // == sqrt(1+x)-1 CAS> 1. // x-1 = 2-1 = 1 CAS> f(Ans) 0.414213562373 0.189207115003 9.05077326653e−2 4.42737824274e−2 2.18971486541e−2 1.08892860517e−2 The recursive formula, log1p(x) = 2*log1p(x/(sqrt(1+x)+1)), is similar to atan formula (05-31-2021 09:51 PM)Albert Chan Wrote: \(\displaystyle\arctan(x) = 2\arctan\left( {x \over \sqrt{1+x^2}+1} \right)\) RE: Ln(x) using repeated square root extraction - Thomas Klemm - 03-06-2022 09:33 AM I was curious to try Pauli's idea and use: \(\log\left(\frac{1+\varepsilon}{1-\varepsilon}\right) = 2 \left( \varepsilon + \frac{\varepsilon^3}{3} + \frac{\varepsilon^5}{5} + \mathcal{O}(\varepsilon^7) \right) \) We set: \(\frac{1+\varepsilon}{1 - \varepsilon} = x\) This leads to: \(\varepsilon = \frac{x - 1}{x + 1}\) Here's the corresponding program for the HP-15C: Code: 001 { 11 } √x̅ The computation of \(\varepsilon\) is now a bit more complicated, but we have fewer terms to compute. In the end, the program is shorter. The result for \(x = 2\) is: 0.6931471773 Compared to the previous result, it is only slightly off in the last digit. RE: Ln(x) using repeated square root extraction - Albert Chan - 03-09-2022 07:47 PM Instead of reducing argument, √...√x, we can blow it up, and get log from AGM From Series[EllipticK[1-(4x)^2], {x,0,4}], and ignored imaginery parts: K(m=1-(4/x)^2) = log(x) + (log(x)-1)*(4/x^2) + O(1/x^4) lua> x = 2^14 lua> a, b = 1, 4/x lua> c = b/x lua> for i=1,6 do a,b = (a+b)/2, sqrt(a*b); print(a,b) end Code: 0.5001220703125 0.015625 AGM have quadratic convergence. With 6 sqrt, (a,b) already close enough so that (a+b)/2 ≈ AGM lua> k = pi/(a+b) lua> k / 14 0.6931471898242749 lua> (k+c)*(1-c) / 14 0.6931471805599455 lua> log(x) / 14 -- = log(2) 0.6931471805599453 Note: if c=4/x^2 small enough, below machine epsilon, log(x) = k, no need for correction. RE: Ln(x) using repeated square root extraction - Thomas Klemm - 03-10-2022 05:18 AM In the initial thread An old logarithm algorithm the "pages 33 and 34 of the manual for the Texas Instruments SR-10" are quoted. However in this manual for the Texas Instruments electronic slide rule calculator SR-10 we find on page 30: Quote:Logarithmic and Exponential Function The first 3 terms of the Taylor series of this expression agree with those of \(\log\left(\frac{1+\varepsilon}{1-\varepsilon}\right) \): \( \frac{2\varepsilon}{9} \left( 4 + \frac{5}{1 - \frac{3\varepsilon^2}{5}} \right) = 2 \varepsilon + \frac{2\varepsilon^3}{3} + \frac{2\varepsilon^5}{5} + \frac{6\varepsilon^7}{25} + \frac{18\varepsilon^9}{125} + \mathcal{O}(\varepsilon^{11}) \) Again a program for the HP-15C: Code: 001 { 11 } √x̅ The result for \(x = 2\) is: 0.6931471786 RE: Ln(x) using repeated square root extraction - Albert Chan - 03-10-2022 03:17 PM (03-10-2022 05:18 AM)Thomas Klemm Wrote: The first 3 terms of the Taylor series of this expression agree with those of \(\log\left(\frac{1+\varepsilon}{1-\varepsilon}\right) \):FYI, approximation formula based from atanh(ε) pade approximation: atanh(ε) = ε + ε^3/3 + ε^5/5 + ... ≥ ε + (ε^3/3) / (1 - 3/5*ε^2) Let d = 1 - 3/5*ε^2, we have ε^2/3 = 5/9*(1-d) ε + (ε^3/3)/d = ε*(1 + 5/9*(1-d)/d) = ε/9*(9 + 5*(1/d-1)) = ε/9*(4 + 5/d) ln((1+ε)/(1-ε)) = 2*atanh(ε) ≥ 2ε/9*(4 + 5/(1-3/5*ε^2)) (10-22-2021 02:15 PM)Albert Chan Wrote: ln(n) ≈ g - g/(2.7 + 24/g^2) , where g = (n-1)/√n (*) We doubled accuracy if we use above formula instead (Bonus, less code steps) Again, taylor series of ln((1+ε)/(1-ε)), using above formula for ln: XCAS> g := (x-1)/sqrt(x) XCAS> f := g - g/(27/10+24/g^2) XCAS> series(f(x=(1+ε)/(1-ε)),ε,0,10) 2*ε + 2/3*ε^3 + 2/5*ε^5 + 123/400*ε^7 + 3217/12000*ε^9 + O(ε^11) XCAS> (c - 6/25) / (c - 123/400) | c=2/7. -2.09836065574 |