6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Nov 23, 2024 7:09 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri Nov 10, 2017 4:37 pm 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
The natural logarithm of 3 is not a particularly important mathematical constant, but there is a formula for it that allows it to be computed with a spigot algorithm (this formula can be derived from the Taylor series for ln(1+x)):

Code:
   1+x              x^3   x^5   x^7
ln(---) =  2 * (x + --- + --- + --- + ...)
   1-x               3     5     7


Plugging in x = 1/2, we get:

Code:
        2      2         2         2
ln(3) = - + ------- + ------- + ------- + ...
        2   3 * 2^3   5 * 2^5   7 * 2^7


We can write this in "spigot" form (for lack of a better phrase), as follows (note that the 3,5,... in the numerators cancel out the 3,5,... from the previous denominator):

Code:
             1        3        5
ln(3) = 1 + --- (1 + --- (1 + --- (1 + ...
            3*4      5*4      7*4


Here is the ln(3) spigot program (actually, it outputs 1. and then the spigot is only on the fractional part), similar to the pi spigot, but of course with different terms (as usual, update OUTPUT for your system to output the character in A):

Code:
{ 8000          }    ORG $8000

{ 8000          } :LN3
{ 8000 D8       }    CLD
{ 8001 20 61 80 }    JSR =.I
{ 8004 A9 31    }    LDA # $31
{ 8006 20 BD 80 }    JSR =OUTPUT
{ 8009 A9 2E    }    LDA # $2E
{ 800B 20 BD 80 }    JSR =OUTPUT
{ 800E A2 14    }    LDX # 21 1 -
{ 8010 8A       } .1 TXA
{ 8011 48       }    PHA
{ 8012 A9 00    }    LDA # 0
{ 8014 8D BB 80 }    STA =Q
{ 8017 A2 1F    }    LDX # 31
{ 8019 8A       } .2 TXA
{ 801A 38       }    SEC
{ 801B 2A       }    ROL A
{ 801C 20 A3 80 }    JSR =MUL
{ 801F 48       }    PHA
{ 8020 AD BB 80 }    LDA =Q
{ 8023 48       }    PHA
{ 8024 A9 0A    }    LDA # 10
{ 8026 8D BB 80 }    STA =Q
{ 8029 BD 6B 80 }    LDA =N 1 - ,X
{ 802C 20 A3 80 }    JSR =MUL
{ 802F 8D BC 80 }    STA =R
{ 8032 68       }    PLA
{ 8033 6D BB 80 }    ADC =Q
{ 8036 8D BB 80 }    STA =Q
{ 8039 68       }    PLA
{ 803A 6D BC 80 }    ADC =R
{ 803D 8E BC 80 }    STX =R
{ 8040 38       }    SEC
{ 8041 2E BC 80 }    ROL =R
{ 8044 0E BC 80 }    ASL =R
{ 8047 0E BC 80 }    ASL =R
{ 804A 20 8B 80 }    JSR =DIV
{ 804D 9D 6B 80 }    STA =N 1 - ,X
{ 8050 CA       }    DEX
{ 8051 D0 C6    }    BNE =.2
{ 8053 AD BB 80 }    LDA =Q
{ 8056 49 30    }    EOR # $30
{ 8058 20 BD 80 }    JSR =OUTPUT
{ 805B 68       }    PLA
{ 805C AA       }    TAX
{ 805D CA       }    DEX
{ 805E D0 B0    }    BNE =.1
{ 8060 60       }    RTS
{ 8061 A2 1E    } .I LDX # 30
{ 8063 A9 01    }    LDA # 1
{ 8065 9D 6C 80 } .J STA =N ,X
{ 8068 CA       }    DEX
{ 8069 10 FA    }    BPL =.J
{ 806B 60       }    RTS
{ 806C          } :N DS 31

{ 808B          } :DIV ; 256*a+q / r -> t = b * q + a
{ 808B A0 08    }    LDY # 8
{ 808D 0E BB 80 }    ASL =Q
{ 8090 2A       } .1 ROL A
{ 8091 B0 05    }    BCS =.2
{ 8093 CD BC 80 }    CMP =R
{ 8096 90 04    }    BCC =.3
{ 8098 ED BC 80 } .2 SBC =R
{ 809B 38       }    SEC
{ 809C 2E BB 80 } .3 ROL =Q
{ 809F 88       }    DEY
{ 80A0 D0 EE    }    BNE =.1
{ 80A2 60       }    RTS
{ 80A3          } :MUL ; a * q -> 256*a+q
{ 80A3 8D BC 80 }    STA =R
{ 80A6 A9 00    }    LDA # 0
{ 80A8 A0 08    }    LDY # 8
{ 80AA 4E BB 80 }    LSR =Q
{ 80AD 90 04    } .1 BCC =.2
{ 80AF 18       }    CLC
{ 80B0 6D BC 80 }    ADC =R
{ 80B3 6A       } .2 ROR A
{ 80B4 6E BB 80 }    ROR =Q
{ 80B7 88       }    DEY
{ 80B8 D0 F3    }    BNE =.1
{ 80BA 60       }    RTS
{ 80BB          } :Q DS 1
{ 80BC          } :R DS 1

{ 80BD          } :OUTPUT
{ 80BD 49 80    }    EOR # $80
{ 80BF 4C ED FD } .A JMP $FDED


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2017 5:28 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8544
Location: Southern California
Bruce, our resident algorithms expert, I sincerely appreciate your contributions. I suspect you know about the following and perhaps decided it's easier to express the equations in text in [code]...[/code] blocks, but just in case (and for the benefit of others too): I understand many desktop-publishing software packages have equation editors, but there's also http://www.codecogs.com/latex/about.php (click on "Full Panel Range (Click to open Standalone Equation Editor)") that lets you make typeset equations and get images or html to copy. Here's your first one, done in CodeCogs:
Attachment:
CodeCogsEqn1.png
CodeCogsEqn1.png [ 9.43 KiB | Viewed 1292 times ]

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 13, 2017 1:56 am 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
The image looks great, but the reason to use text is that it is (more) easily searchable. How would a search for say 1+x find that image? These days, I'm in the camp that says use text unless it is absolutely unreadable. (Text isn't perfect either, of course.) A very, very small percentage of web pages contain mathematical formulas, so it may be a long time before there's a standard, commonly used, way of rendering them that allows them to be searched in a consistent way.


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 13, 2017 2:46 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8544
Location: Southern California
The searching is a good reason. You could do both (maybe super tiny text for searching and the image for readability), but it'd just be more work to post. The LaTeX html output would be searchable if we could post html.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 13, 2017 9:31 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
(I think the way to tackle maths typesetting on the web is MathJax. There is a phpBB module for it, but I doubt if there's enough demand on this board for Mike to wrestle with installing it.)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: