Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
|
11-13-2019, 05:40 PM
(This post was last modified: 11-23-2024 01:59 PM by Gerson W. Barbosa.)
Post: #20
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
Former forum member Mike (Stgt) sent me two RPN programs which give perfect 10-digit and 12-digit results instantaneously on RPN calculators, inspired in Paul Dale's suggestion in message #4 of this thread:
01 LBL "LN2" 02 66 03 2 04 RCL Y 05 1/X 06 Y^X 07 RCL X 08 1 09 ST- Z 10 + 11 / 12 ENTER 13 X^2 14 3 15 ST+ Y 16 / 17 * 18 * 19 ST+ X 20 END That's log(2) = 132*atanh((2^(1/66) - 1)/(1 + 2^(1/66))), evaluated to two terms of the atanh(x) series: 132*((2^(1/66) - 1)/(1 + 2^(1/66)) + 1/3*((2^(1/66) - 1)/(1 + 2^(1/66)))^3) = 0.6931471806, on the HP-41 01▶LBL "LN2" 02 74 03 2 04 RCL ST Y 05 1/X 06 Y^X 07 RCL ST X 08 1 09 STO- ST Z 10 + 11 ÷ 12 ENTER 13 X^2 14 3 15 STO+ ST Y 16 ÷ 17 × 18 × 19 STO+ ST X 20 END That's log(2) = 148*atanh((2^(1/74) - 1)/(1 + 2^(1/74))), evaluated to two terms of the atanh(x) series: 148*((2^(1/74) - 1)/(1 + 2^(1/74)) + 1/3*((2^(1/74) - 1)/(1 + 2^(1/74)))^3) = 0.693147180560, on the HP-42S Inspired in Mike's contribution, I've written two HP-42S programs that give perfect results, either on the HP-42S or on Free42S: --------------------------------------------------- 00 { 52-Byte Prgm } 01▸LBL "Ln2" 02 3 03 2 04 SQRT 05 FP 06 1 07 RCL+ ST L 08 ÷ 09 STO 00 10 X↑2 11 LASTX 12▸LBL 00 13 RCL× ST Y 14 RCL÷ ST Z 15 STO+ 00 16 X<> ST L 17 X<> ST Z 18 SIGN 19 STO+ ST X 20 RCL+ ST L 21 X<> ST Z 22 DSE ST T 23 GTO 00 24 4 25 RCL× 00 26 .END. 6 XEQ Ln2 -> 0,69314718056 (under 2 seconds on the HP-42S) 21 XEQ Ln2 -> 0,6931471805599453094172321214581760 (Free42) 2 LN -> 0,6931471805599453094172321214581766 (Free42) --------------------------------------------------- 00 { 55-Byte Prgm } 01▸LBL "Ln2" 02 3 03 2 04 SQRT 05 SQRT 06 1 07 - 08 2 09 RCL+ ST Y 10 ÷ 11 STO 00 12 X↑2 13 LASTX 14▸LBL 00 15 RCL× ST Y 16 RCL÷ ST Z 17 STO+ 00 18 X<> ST L 19 X<> ST Z 20 SIGN 21 STO+ ST X 22 RCL+ ST L 23 X<> ST Z 24 DSE ST T 25 GTO 00 26 8 ; double this constant after each additional SQRT 27 RCL× 00 28 .END. 4 XEQ Ln2 -> 0,69314718055 (HP-42S) 14 XEQ Ln2 -> 0,6931471805599453094172321214581766 (Free42) 2 LN -> 0,6931471805599453094172321214581766 (Free42) --------------------------------------------------- This approach has allowed me to get 999 correct digits in 0.03s on my computer, with Decimal BASIC: OPTION ARITHMETIC DECIMAL_HIGH LET nd = 1000 PRINT "Log(2) =" LET t = TIME LET k = 298 LET r = TIME - t LET a = SQR(SQR(SQR(SQR(2)))) LET x = (a - 1)/(a + 1) LET d = 1 LET s = x LET a = x*x FOR i = 1 TO k LET d = d + 2 LET x = x*a LET s = s + x/d NEXT i LET s = 32*s LET r = TIME - t LET r$ = STR$(s) PRINT "0"; PRINT r$(0:1); FOR i = 2 TO nd + 1 PRINT r$(i:i); IF MOD((i - 1),10) = 0 THEN PRINT " "; IF MOD((i - 1),50) = 0 THEN PRINT " "; END IF NEXT i IF MOD (i - 2,50) <> 0 OR nd = 0 THEN PRINT PRINT "Runtime: "; PRINT USING "0.##": r; PRINT " seconds" END --------------------------------------------------- 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 2344535343 Runtime: 0.03 seconds Thank you, Pauli and Mike, for your contributions! Gerson. Edited to fix a typo in line 7 of Mike’s first RPN program (RCL X, not RCL Y). Also, 1 E in line 8 has been replaced with 1, as in his second program. That is one byte shorter and doesn’t cause Free42 to show an “Internal Error” message upon execution, when being pasted to it. Somehow Free42 3.1.10 doesn’t accept the constant 1E or 1 E, unless these are entered manually. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)