Post Reply 
Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
10-12-2019, 12:32 AM
Post: #1
Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
My goal is finding a fast and short way to compute the natural logarithm of 2 on the HP-15C and other RPN calculators like the HP-42S, without using any built-in function (LN, LOG, ATANH...). 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.

I have provided two RPN programs and a Decimal BASIC program. The latter avoids any multiplication operations inside the loop, except for the two necessary divisions. I would like to have tried it on the HP-11C which is slightly faster, but currently I have none.

Later I will post the (partially original) formula I have used, in case it is not so obvious from the listings.

You are invited to contribute code for other RPN calculators (HP-41C, HP-32S, HP-12C...), based on this algorithm or improved versions thereof, or based on more efficient ones (plenty of formulas in the above hyperlink). Pseudo code and short BASIC programs are also welcome.

Thanks in advance for your interest and contribuitions.

Gerson.

======================================

HP-15C

======================================


001-    42 21 11  f LBL A
002-       44 25    STO I
003-           4    4
004-          20    ×
005-           1    1
006-          40    +
007-       44  1    STO 1
008-           0    0
009-       44  0    STO 0
010-    42 21  0  f LBL 0
011-    45 40  1    RCL + 1
012-       45 25    RCL I
013-       43 11  g x²
014-          34    x↔y
015-          10    ÷
016-       45 25    RCL I
017-    45 40 25    RCL + I
018-       43 11  g x²
019-       43 36  g LSTx
020-          30    -
021-          15    1/x
022-    44 40  0    STO + 0
023-          34    x↔y
024-    42  5 25  f DSE I
025-       22  0    GTO 0
026-    45 40  1    RCL + 1
027-          15    1/x
028-    45 40  0    RCL + 0
029-       43 32  g RTN



4 f A -> 0.6931471808 ( ~  8.5 seconds )

5 f A -> 0.6931471805 ( ~ 10.0 seconds )


======================================

HP-42S/Free42

======================================


00 { 48-Byte Prgm }
01▸LBL "Ln2"
02 RCL ST X
03 4
04 ×
05 1
06 +
07 0
08 STO 00
09▸LBL 00
10 RCL+ ST Y
11 RCL ST Z
12 X↑2
13 X<>Y
14 ÷
15 RCL ST Z
16 STO+ ST X
17 X↑2
18 RCL- ST L
19 1/X
20 STO+ 00
21 R↓
22 DSE ST Z
23 GTO 00
24 +
25 1/X
26 RCL+ 00
27 END

 6 XEQ Ln2 -> 0.69314718056 ( ~ 2 seconds, HP-42S ) 

16 XEQ Ln2 -> 0.6931471805599453094172321214581766 ( Free42 )

======================================

Decimal BASIC

======================================

OPTION ARITHMETIC DECIMAL_HIGH
! Log(2)
INPUT PROMPT "Number of decimal places: ":nd
LET t = TIME 
LET k = CEIL(ND/LOG(8))                                                         
LET n = k*k
LET m = 2*k - 1                                                   
LET c = 8                                                                                                                                                                                             
LET d0 = c*(k - 2) + 10 
LET d1 = 2*k*(2*k - 1)
LET d2 = 4*k + 1                                         
LET s1 = 0  
LET s2 = 0                                                                                                 
FOR i = 1 TO k                                                                                       
   LET s1 = s1 + 1/d1                                                  
   LET d1 = d1 - d0                     
   LET d0 = d0 - c
   LET s2 = n/(s2 + d2)                                               
   LET n = n - m
   LET m = m - 2
NEXT i
LET s2 = 1/(s2 + d2)
LET r = TIME - t 
LET r$ = STR$(s1 + s2)                           
PRINT
PRINT " ";
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
      PRINT "  ";
   END IF
NEXT i
IF MOD (i - 2,50) <> 0  OR nd = 0 THEN PRINT
PRINT 
PRINT "Runtime: ";
PRINT  USING "0.##": r;
PRINT " seconds"
END


Number of decimal places: 1000

 .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.32 seconds
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others] - Gerson W. Barbosa - 10-12-2019 12:32 AM



User(s) browsing this thread: 1 Guest(s)