Post Reply 
Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
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.

Late to the party and a bit surprised nobody came up with the following solution:

HP-15C

Code:
001 - 42,21,15  LBL E      
002 -       15  1/x        
003 -    43 32  RTN

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

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'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.

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:

OPTION ARITHMETIC DECIMAL_HIGH
LET n = 2^24
LET nd = 1000
PRINT "LB(10) ="
LET t = TIME
LET g = 0.6931471805599453                            !'ln(2) 16 digits
LET h = g/n
LET a = h*h
LET x = 1
LET s = 1 - h                                         !'s = exp(-h)
FOR k = 3 TO 111 STEP 2                               !'loops = 55
   LET x = x * a/(k*k-k)
   LET s = s + x*(k-h)                                !'2 terms at a time
NEXT k
LET s = s^n - 0.5                                     !'s = exp(-g) - 0.5
LET x = s/(s+1)                                       !'ln(1+2s) = 2*atanh(x)
LET a = x*x
LET s = x
FOR k = 3 TO 57 STEP 2                                !'loops = 28
   LET x = x*a
   LET s = s + x/k
NEXT k
LET ln = g + 2*s                                      !ln(2) = g + 2*atanh(x)
LET p1 = 1E643
LET p2 = 2^2136
LET xx = (p2 - p1)/(p2 + p1)
LET x2 = xx*xx
LET sm = 0
LET ni = nd/(LOG(1/xx)/LOG(10)) - 1/2 
FOR nn = 1 TO ni STEP 2
   LET sm = sm + xx/nn
   LET xx = xx*x2
NEXT nn
LET lb = (2136 - (sm + sm)/ln)/643                    !LB(10) = (2136 - 2*sm/ln(2))/643
LET r = TIME - t
LET r$ = STR$(lb - INT(lb))
PRINT
PRINT STR$(INT(lb));
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 REPEAT$(" ",1 + LEN(STR$(INT(lb))));
   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

--------------------------------------------------------

LB(10) =

3.3219280948 8736234787 0319429489 3901758648 3139302458 
  0612054756 3958159347 7660862521 5850139743 3593701550 
  9965737171 0250251826 8240969842 6352688827 5302772998 
  6553938519 5135265750 5568643017 6091900248 9166694143 
  3374011903 1241873751 0971586646 7540179189 6558067358 
  3077968843 2725883274 9925224489 0238355997 6417394137 
  9280097727 5668635547 7901486745 0578458847 8027104225 
  4560972234 6579569554 1537019157 6411717792 4716513500 
  2392112714 7339361440 7233972115 7485100709 4987891658 
  8808313221 9480679329 8232325931 1950671399 5078370033 
  6734248070 6635275008 4069176263 8625354688 0153686216 
  1841886085 8994835381 3214998930 2704417920 7865922601 
  8229653715 7536723966 0695116486 8368466238 5850848606 
  2990542699 4692791162 7320613400 6446704847 6340704373 
  5233674221 2830896703 6457909216 7721909021 4219621424 
  5744465852 4535948448 8154834592 5142954093 7353906549 
  4486327792 9842429159 1181131163 2981257694 5019815750 
  3792185538 4878203551 6019737827 7288881759 8743328660 
  7271239382 5202213332 8052551248 8274344488 4245316546 
  5061241489 1822867932 5266429281 1659922851 627345082 
  
Runtime: 0.03 seconds

--------------------------------------------------------

Best regards,

Gerson.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others] - Gerson W. Barbosa - 06-15-2022 05:08 PM



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