Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
|
11-14-2019, 12:04 AM
Post: #21
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(11-11-2019 06:14 PM)Gerson W. Barbosa Wrote: n = 1663 will be enough for 1000 digits: It may be better to make n even, avoiding 1 square root: \(\Large \frac{\pi}{\log 2}\normalsize ≈ AGM(2n, \frac{2n}{2^{n-2}}) = AGM(n + \frac{n}{2^{n-2}}, \frac{2n}{2^{n/2-1}}) \) With n=1662, loop count down to 17. Code: OPTION ARITHMETIC DECIMAL_HIGH |
|||
11-14-2019, 12:12 AM
Post: #22
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others] | |||
11-14-2019, 02:01 AM
Post: #23
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(11-14-2019 12:04 AM)Albert Chan Wrote:(11-11-2019 06:14 PM)Gerson W. Barbosa Wrote: n = 1663 will be enough for 1000 digits: I`ve just tested your optmization. For conciseness, here are the last lines of the ouput: ... 7891923723 1467232172 0534016492 5687274778 2344535347 Runtime: 0.03 seconds The running times vary, depending on whatever other tasks the OS is doing. The ones I've posted are the minimum values I've observed for each example. This appears to be the record here for one thousand correct digits. |
|||
11-14-2019, 05:34 PM
Post: #24
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
Another way to speedup log(2) calculations is to use ... uh ... log(2)
Let r = roughly the value of log(2) log2 = r + log2 - r = r + log(2 exp(-r)) Last term should be tiny: log(2 exp(-r)) = log(1 + ε) = 2 atanh(x) With r = 0.6931471805599453, x = ε/(ε+2) ≈ 4.7e-18 → atanh(x) converge very quickly, +35 decimal digits per loop. Code: OPTION ARITHMETIC DECIMAL_HIGH |
|||
11-14-2019, 11:24 PM
(This post was last modified: 11-14-2019 11:29 PM by Gerson W. Barbosa.)
Post: #25
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(11-14-2019 05:34 PM)Albert Chan Wrote: In decimal BASIC you can also use LET r = LOG(2) !'log2 16 digits Either way, the output is Log(2) = 0.6931471805 5994530941 7232121458 1765680755 0013436025 5254120680 0094933936 2196969471 5605863326 9964186875 4200148102 0570685733 6855202357 5813055703 2670751635 0759619307 2757082837 1435190307 0386238916 7347112335 0115364497 9552391204 7517268157 4932065155 5247341395 2588295045 3007095326 3666426541 0423915781 4952043740 4303855008 0194417064 1671518644 7128399681 7178454695 7026271631 0645461502 5720740248 1637773389 6385506952 6066834113 7273873722 9289564935 4702576265 2098859693 2019650585 5476470330 6793654432 5476327449 5125040606 9438147104 6899465062 2016772042 4524529612 6879465461 9316517468 1392672504 1038025462 5965686914 4192871608 2938031727 1436778265 4877566485 0856740776 4845146443 9940461422 6031930967 3540257444 6070308096 0850474866 3852313818 1676751438 6674766478 9088143714 1985494231 5199735488 0375165861 2753529166 1000710535 5824987941 4729509293 1138971559 9820565439 2871700072 1808576102 5236889213 2449713893 2037843935 3088774825 9701715591 0708823683 6275898425 8918535302 4363421436 7061189236 7891923723 1467232172 0534016492 5687274778 2344535347 Runtime: 0.02 seconds |
|||
11-15-2019, 10:58 AM
Post: #26
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(11-14-2019 05:34 PM)Albert Chan Wrote: Another way to speedup log(2) calculations is to use ... uh ... log(2) It's not good practice to reuse a variable other than loop indices. Since r is already used for the run time, you should use some variable other than r for LOG(2). Tom L Cui bono? |
|||
11-17-2019, 03:01 PM
Post: #27
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(11-15-2019 10:58 AM)toml_12953 Wrote: It's not good practice to reuse a variable other than loop indices. This faster version renamed variable r as g. To speedup exp(-g): exp(-g) = exp(-g/n) ^ n, where n = 2^24 This reduce exp() loop count from 212 down to 55. Code: OPTION ARITHMETIC DECIMAL_HIGH |
|||
11-19-2019, 10:04 PM
Post: #28
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(11-17-2019 03:01 PM)Albert Chan Wrote: To speedup exp(-g): exp(-g) = exp(-g/n) ^ n, where n = 2^24 Output: Log(2) = 0.6931471805 5994530941 7232121458 1765680755 0013436025 5254120680 0094933936 2196969471 5605863326 9964186875 4200148102 0570685733 6855202357 5813055703 2670751635 0759619307 2757082837 1435190307 0386238916 7347112335 0115364497 9552391204 7517268157 4932065155 5247341395 2588295045 3007095326 3666426541 0423915781 4952043740 4303855008 0194417064 1671518644 7128399681 7178454695 7026271631 0645461502 5720740248 1637773389 6385506952 6066834113 7273873722 9289564935 4702576265 2098859693 2019650585 5476470330 6793654432 5476327449 5125040606 9438147104 6899465062 2016772042 4524529612 6879465461 9316517468 1392672504 1038025462 5965686914 4192871608 2938031727 1436778265 4877566485 0856740776 4845146443 9940461422 6031930967 3540257444 6070308096 0850474866 3852313818 1676751438 6674766478 9088143714 1985494231 5199735488 0375165861 2753529166 1000710535 5824987941 4729509293 1138971559 9820565439 2871700072 1808576102 5236889213 2449713893 2037843935 3088774825 9701715591 0708823683 6275898425 8918535302 4363421436 7061189236 7891923723 1467232172 0534016492 5687274778 2344535347 Runtime: 0.00 seconds |
|||
11-20-2019, 03:45 AM
Post: #29
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(11-17-2019 03:01 PM)Albert Chan Wrote: This faster version renamed variable r as g. Wow! That's some reduction. Good job! Tom L Cui bono? |
|||
03-23-2020, 04:34 PM
(This post was last modified: 03-23-2020 07:07 PM by Albert Chan.)
Post: #30
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
Inspired by Valentin Albillo post on estimating Pi using random numbers.
And, the proof for it: https://math.stackexchange.com/questions...379#885379 LN(2) = 2 * probability of integer part of RND/RND is odd 10 INPUT K @ N=0 @ FOR I=1 TO K @ N=N+MOD(IP(RND/RND),2) @ NEXT I @ DISP 2*N/K @ GOTO 10 > RANDOMIZE 1 > RUN ? 1E3 → .68 ? 1E3 → .756 ? 1E4 → .6806 ? 1E4 → .6938 |
|||
03-24-2020, 06:24 AM
Post: #31
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(03-23-2020 04:34 PM)Albert Chan Wrote: Inspired by Valentin Albillo post on estimating Pi using random numbers.Oh, very many thanks for that link! A simple proof and exactly the graphic that I've been wanting to see! Quote:LN(2) = 2 * probability of integer part of RND/RND is oddGoodness me! |
|||
03-28-2020, 01:31 PM
(This post was last modified: 03-28-2020 03:05 PM by Albert Chan.)
Post: #32
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(03-23-2020 04:34 PM)Albert Chan Wrote: LN(2) = 2 * probability of integer part of RND/RND is odd We can improve LN(2) estimate by scaling RND/RND Let p(m) = probability of integer part of m*RND/RND is odd XCas> [log2] := expand(solve(p(m) = floor(m/2)/(2*m) + (m/2)*(log2 + c), log2)) → [2*p(m)/m-floor(m/2)/m^2-c] XCas> c := (m>1)*sum((-1)^k/k, k=1..2*floor(m/2)) XCas> expand(subst(log2, m=1)) → 2*p(1) XCas> expand(subst(log2, m=2)) → p(2)+1/4 XCas> expand(subst(log2, m=3)) → 2*p(3)/3+7/18 XCas> expand(subst(log2, m=10)) → p(10)/5+1501/2520 > 10 DEF FNL(K) @ N=0 > 20 FOR L1=1 TO K @ N=N+MOD(IP(10*RND/RND),2) @ NEXT L1 > 30 FNL=N/(5*K)+1501/2520 @ END DEF > RUN > FIX 5 @ RANDOMIZE 1 > FOR I=1 to 5 @ FNL(1e4) @ NEXT I 0.69063 0.69441 0.69329 0.69347 0.69377 |
|||
06-12-2022, 12:17 PM
Post: #33
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(10-12-2019 12:32 AM)Gerson W. Barbosa Wrote: By fast I mean under 10 seconds on the HP-15C for maximum possible precision. By short I mean no more than 30 or 40 RPN steps. Late to the party and a bit surprised nobody came up with the following solution: HP-15C Code: 001 - 42,21,15 LBL E 1 ENTER 2 ∫ E With FIX 9 it takes about a minute to get: 0.693147181 While CLEAR PREFIX will show: 6931471805 We can compare it to the correct value: 2 LN 0.693147181 But CLEAR PREFIX shows: 6931471806 We can repeat the calculation with FIX 4 and get after about 15 seconds: 0.6931 Here CLEAR PREFIX shows: 6931456394 Interestingly the result is the same with FIX 3 and FIX 5. HP-42S Code: 00 { 14-Byte Prgm } We can use ∫ f(x) with the following parameters: LLIM = 1 ULIM = 2 ACC = 1E-34 The result on Free42 after about 1 second is: 6.931471805599453094172321214581642e-1 Compare it to the correct value: 6.931471805599453094172321214581766e-1 While these solutions may not be considered fast enough they are probably the shortest. (10-12-2019 12:32 AM)Gerson W. Barbosa Wrote: without using any built-in function (LN, LOG, ATANH...) I'm aware that calculating an integral is a built-in function, but then calculating the square root is as well. Thus I assumed that this rule should prevent a trivial way to do it. But then you may consider this a trivial solution as well. |
|||
06-15-2022, 05:08 PM
Post: #34
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(06-12-2022 12:17 PM)Thomas Klemm Wrote:(10-12-2019 12:32 AM)Gerson W. Barbosa Wrote: By fast I mean under 10 seconds on the HP-15C for maximum possible precision. By short I mean no more than 30 or 40 RPN steps. One minute is way longer than 10 seconds, but not too long. Also, on my HP-15C LE it would take no more than half a second. (06-12-2022 12:17 PM)Thomas Klemm Wrote:(10-12-2019 12:32 AM)Gerson W. Barbosa Wrote: without using any built-in function (LN, LOG, ATANH...) I am not good at setting up rules. Loopholes are always likely to occur. Too good I left Law School after just one week :-) Now that we are back at this topic, early in the beginning of the pandemic I was asked to find a fast way to compute the binary log of 10 (3.32192809...). I don't know any specific binary log algorithm, but since Albert Chan had already provided a method for instantly computing the natural logarithm of 2, I came up with a solution that yields slightly more than 8 digits per iteration. Probably there are more efficient ways to do it, but I will publish the DECIMAL BASIC code and the first 1000 digits of the constant here just for the record. Too few remarks as I haven't kept any notes. Code:
Best regards, Gerson. |
|||
06-19-2022, 02:10 PM
Post: #35
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
This program for the HP-42S uses formula (19):
\( \begin{align} \ln 2= \sum_{k=1}^{\infty} \frac{1}{k \cdot 2^k} \end{align} \) Code: 00 { 14-Byte Prgm } 37 R/S 0.693147180559758517350355291780403 0.6931471805599453094172321214581766 105 R/S 0.6931471805599453094172321214581765 0.6931471805599453094172321214581766 I have no idea how long it takes on a real HP-42S. |
|||
06-19-2022, 03:46 PM
(This post was last modified: 06-19-2022 03:54 PM by C.Ret.)
Post: #36
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(06-19-2022 02:10 PM)Thomas Klemm Wrote: This program for the HP-42S uses formula (19): I like the way Thomas get rid of the \( 2^k \) factor by using a factorized division by 2 in his code. This spare several program steps and globally speed up all the process. So, I immediately convert his program for my HP-15C: Code: 001 - { 44,25 } STO I The fastest and accurate result obtained with 27 as argument : f FIX 9 f PRGM 27 R/S display 0.693147181 in least than 23"97. The f PREFIX reveals digits 6931471805 |
|||
06-19-2022, 07:07 PM
Post: #37
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
We can also use Paul's idea and use the Taylor series of:
\( \begin{align} \log\left(\frac{1+x}{1-x}\right) &= 2 x + 2 x^3 \frac{1}{3} + 2 x^5 \frac{1}{5} + 2 x^7 \frac{1}{7} + 2 x^9 \frac{1}{9} + 2 x^{11} \frac{1}{11} + \cdots \\ &= 2 x \left[1 + x^2 \frac{1}{3} + x^4 \frac{1}{5} + x^6 \frac{1}{7} + x^8 \frac{1}{9} + x^{10} \frac{1}{11} + \cdots\right] \\ \end{align} \) For \(x\) we solve the equation: \( \begin{align} \frac{1+x}{1-x} = \sqrt[16]{2} \end{align} \) This leads to: \( \begin{align} x = \frac{\sqrt[16]{2} - 1}{\sqrt[16]{2} + 1} \end{align} \) We could maybe increase \(16\) to further reduce \(x\) and thus speed up the summation. However we experience cancelation in the numerator. I haven't checked the influence yet. Thus for now \(16\) is just a wild guess. Initialization R00: \(x^2\) R01: \(32x\) 1 ENTER 2 SQRT SQRT SQRT SQRT + LASTX 1 - X<>Y / X↑2 STO 00 LASTX 32 * STO 01 Programs HP-42S: Code: 00 { 18-Byte Prgm } HP-15C: Code: 000 { } Examples 3.00002 R/S 0.6931471500554851572008806023721818 5.00002 R/S 0.693147180549725015633085213078373 7.00002 R/S 0.6931471805599415808474258676300801 9.00002 R/S 0.6931471805599453079863200827178153 11.00002 R/S 0.6931471805599453094166642087519808 13.00002 R/S 0.6931471805599453094172318905964119 15.00002 R/S 0.6931471805599453094172321213626303 17.00002 R/S 0.6931471805599453094172321214581366 19.00002 R/S 0.6931471805599453094172321214581768 2 LN 0.6931471805599453094172321214581766 |
|||
06-19-2022, 07:57 PM
(This post was last modified: 06-19-2022 09:13 PM by C.Ret.)
Post: #38
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(10-12-2019 01:30 PM)ttw Wrote: Perhaps the methods used here may be of interest. In this publication, the five point Gauss-Legendre Log (5P-GLLOG) lead to this approximation : \( ln(1+x)\approx \frac{7560x+15120x^2+9870x^3+2310x^4+137x^5}{7560+18900x+16800x^2+6300x^3+900x^4+30x^5} \) One may deduce that \( ln(1+1)\approx \frac{7560+15120+9870+2310+137}{7560+18900+16800+6300+900+30} \) \( ln(2)\approx \frac{34977}{50490}=0.\underline{6931471}57853 \) A better fraction may be proposed only base of the numeric value without any further mathematical or fundamental consideration: \( ln(2)\approx \frac{261740}{377611}= 0.\underline{6931471805}641255154113624867919631578529227167640773176628858799 \cdots \) Best numeric approximation I get on my HP-71B using FRAC$(LOG(2)) which coincidentally correspond to \(ln(2)\approx[0;1,2,3,1,6,3,1,1,2,1,1,1,1,3,10]\) |
|||
12-08-2022, 01:41 PM
(This post was last modified: 12-08-2022 03:23 PM by Albert Chan.)
Post: #39
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
Based from [VA]SRC012C
(12-01-2022 08:27 AM)Werner Wrote: Using the original asymptotic series H(n) = ln(n) + gamma + 1/(2*n) - 1/(12*n^2) + 1/(120*n^4) - ... Sum a shell of b-bits integer reciprocals, subtract RHS, we have estimate for ln(2) Example: for 2-bits integer, 2 and 3: 1/2 + 1/3 - 1/8 - 1/64 + 1/2048 - 1/16384 + 17/1048576 = 2180467/3145728 ≈ 0.693151791890462 Add 1 more asymptote term for good measure, we have: CAS> LN2(b) := sum(1/k,k,2^(b-1),2^b-1) - horner([7936,-272,16,-2,1,1/2^(b+1)],1/2^(2b+2)) CAS> LN2(2) 8721775/12582912 ≈ 0.69314440091451 CAS> LN2(3) 312589669081/450971566080 ≈ 0.693147179539803 CAS> LN2(4) 27463878294130001/39622001018535936 ≈ 0.693147180559656 Double the bits, we doubled number of correct digits. OTTH, double the bits required squaring of number of terms, so this is not efficient. Update: above off by factor of 2, but conclusion still hold. 2^(2b-1) = (2^(b-1))^2 * 2 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)