Small RPN exponential routine
|
01-02-2015, 10:25 PM
Post: #1
|
|||
|
|||
Small RPN exponential routine
This would make more sense on the HP-16C, which lacks exponentiation, but the equivalent program is 23 steps long.
I wrote a 42S version as an optimization exercise and managed to save five steps and two registers (but only two bytes), when compared to the first version. Perhaps more optimization is still possible, but I'll stop here. I'll publish the HP-16C version, in case someone is interested (on the 16C this should be a first step towards a usable exponential function implementation). Code:
Examples: 1 ENTER 14 XEQ EX --> 2.71828182846 3.14159265359 ENTER 22 --> 23.1406926328 1000 ENTER 1400 --> 1.97007111402e434 ( Free 42 only! ) |
|||
01-03-2015, 08:42 AM
(This post was last modified: 01-03-2015 08:48 AM by Thomas Klemm.)
Post: #2
|
|||
|
|||
RE: Small RPN exponential routine | |||
01-03-2015, 10:53 AM
Post: #3
|
|||
|
|||
RE: Small RPN exponential routine | |||
01-03-2015, 05:31 PM
Post: #4
|
|||
|
|||
RE: Small RPN exponential routine
This is my first attempt at natural logarithm using
ln(1 + x) = x - x^2/2 + x^3/3 - x^4/4 + x^5/5 -+ ..., x < 1 There surely is a more obvious method which I cannot see, so I won't bother optimizing it. I am primarily interested in implementing these functions on TurboBCD, but rather than trying to reinvent the wheel I'll look for a ready-made solution. Code:
0.52 ENTER 35 --> -6.53926467407e-1 There are two problems, though: 1) The series does not converge for arguments greater than 1; 2) More and more terms are required as arguments approach to zero. For instance, ln(0.052) will require 525 terms for 12-digit accuracy. In these cases, conversions to and from decimal logarithms will help: ln(0.52) = -6.53926467407e-1 2.30258509299 / --> -2.83996656366e-1 1 - 2.30258509299 * --> -2.95651156041 = ln(0.052) ln(0.52) = -6.53926467407e-1 2.30258509299 / --> -2.83996656366e-1 1 + 2.30258509299 * --> 1.64865862558 = ln(5.2) ln(0.52) = -6.53926467407e-1 2.30258509299 / --> -2.83996656366e-1 2 + 2.30258509299 * --> 3.95124371856 = ln(52) ln(0.52) = -6.53926467407e-1 2.30258509299 / --> -2.83996656366e-1 3 + 2.30258509299 * --> 6.25382881155 = ln(520) |
|||
01-03-2015, 08:33 PM
Post: #5
|
|||
|
|||
RE: Small RPN exponential routine
(01-03-2015 05:31 PM)Gerson W. Barbosa Wrote: There surely is a more obvious method which I cannot see, so I won't bother optimizing it. Using the same method but a little shorter: Code: 00 { 24-Byte Prgm } Cheers Thomas |
|||
01-04-2015, 01:14 AM
(This post was last modified: 01-04-2015 02:06 AM by Gerson W. Barbosa.)
Post: #6
|
|||
|
|||
RE: Small RPN exponential routine
(01-03-2015 08:33 PM)Thomas Klemm Wrote:(01-03-2015 05:31 PM)Gerson W. Barbosa Wrote: There surely is a more obvious method which I cannot see, so I won't bother optimizing it. A "little shorter"? I appreciate your sense of humor :-) Definitely I should have made a second attempt in the first place. I've rewritten it looking at the expression with five terms of my example and obtained the same number of steps, but four bytes longer. Despite having one extra step inside the loop, yours appears to be a little faster (only one measurement before I got "Memory Clear" due to flat batteries). Cheers, Gerson. ln(0.52) = -(0.48*(1 + 0.48*(1/2 + 0.48*(1/3 + 0.48*(1/4 + 0.48/5))))) Code:
P.S.: One step less, but still one byte longer, using one of your ideas: Code:
|
|||
01-20-2015, 07:57 PM
Post: #7
|
|||
|
|||
RE: Small RPN exponential routine
Gerson, Is your exponential routine similar to Toth's? (listed below)
Code: 001 - 43,22, d LBL D |
|||
01-20-2015, 09:24 PM
Post: #8
|
|||
|
|||
RE: Small RPN exponential routine
How about HP-41C versions .... pleeeeeeeease!!
:-) Namir |
|||
01-20-2015, 11:48 PM
(This post was last modified: 01-21-2015 04:06 AM by Gerson W. Barbosa.)
Post: #9
|
|||
|
|||
RE: Small RPN exponential routine
(01-20-2015 07:57 PM)James Dunn Wrote: Gerson, Is your exponential routine similar to Toth's? (listed below) My HP-16C version: Code:
Both have approximately the same size and both use Horner's method. My first HP-16C version of Thomas Klemm's more obvious method is only one step shorter, but I surely haven't converted it properly. Regards, Gerson. P.S.: Contrary to what I stated, Victor Toth's program uses a different method. My bad, sorry! |
|||
01-21-2015, 12:34 AM
(This post was last modified: 01-21-2015 12:35 AM by Namir.)
Post: #10
|
|||
|
|||
RE: Small RPN exponential routine
(01-20-2015 11:48 PM)Gerson W. Barbosa Wrote: My HP-16C version: I think LBL A needs to be followed by an X<>Y command. I am assuming the value for the exponent is in register Y and the number of iterations is in register X. I tried your code on an HP-41C and it worked after inserting X<>Y following LBL A. Namir |
|||
01-21-2015, 12:46 AM
(This post was last modified: 01-21-2015 12:57 AM by Gerson W. Barbosa.)
Post: #11
|
|||
|
|||
RE: Small RPN exponential routine
(01-21-2015 12:34 AM)Namir Wrote:(01-20-2015 11:48 PM)Gerson W. Barbosa Wrote: My HP-16C version: Namir, On the 16C, 1 ENTER 12 GSB A returns 2.718281829. No x<>y need. I'll try it on the 41C. Gerson. P.S.: Instead of X<>Y after LBL A, try STO 01 ; assuming that's your index register; SIGN STO 00 X<>Y ENTER ENTER ENTER ... Of course converting Thomas Klemm's program to the HP-41C is better. |
|||
01-21-2015, 02:36 AM
Post: #12
|
|||
|
|||
RE: Small RPN exponential routine
(01-20-2015 07:57 PM)James Dunn Wrote: Gerson, Is your exponential routine similar to Toth's? Toth's program calculates the partial sums: \(1\) \(1+x\) \(1+x+\frac{x^2}{2}\) \(1+x+\frac{x^2}{2}+\frac{x^3}{6}\) \(1+x+\frac{x^2}{2}+\frac{x^3}{6}+\frac{x^4}{24}\) (...) It returns if the value doesn't change anymore. This value is kept in register 9 while the original x is kept in register 8. The terms to be added are calculated recursively from the previous value: \(a_n=\frac{x}{n}a_{n-1}\) (lines 019-021). The index \(n\) is kept in register I. Horner's method let us rewrite the expression: \(1+x(1+\frac{x}{2}(1+\frac{x}{3}(1+\frac{x}{4}(...)))\) My program calculates that inside out using classic RPN style. Thus the loop is inverted: the index is going down to 0. From a numerical point of view that's favorable as it reduces cancelation: the values that are added are of a similar size. Best is to step through the program using special values like 2 that are easy to follow. HTH Thomas |
|||
01-21-2015, 03:18 AM
Post: #13
|
|||
|
|||
RE: Small RPN exponential routine
(01-20-2015 11:48 PM)Gerson W. Barbosa Wrote: My first HP-16C version of Thomas Klemm's more obvious method is only one step shorter, but I surely haven't converted it properly. This is how I would write my program for the HP-16C: Code: 001 - 43,22, A LBL A Cheers Thomas |
|||
01-21-2015, 03:48 AM
Post: #14
|
|||
|
|||
RE: Small RPN exponential routine
(01-21-2015 03:18 AM)Thomas Klemm Wrote:(01-20-2015 11:48 PM)Gerson W. Barbosa Wrote: My first HP-16C version of Thomas Klemm's more obvious method is only one step shorter, but I surely haven't converted it properly. I'm glad my 22-step version has gone straight to the trash bin :-) Cheers, Gerson. |
|||
01-21-2015, 04:03 AM
Post: #15
|
|||
|
|||
RE: Small RPN exponential routine
(01-20-2015 09:24 PM)Namir Wrote: How about HP-41C versions .... pleeeeeeeease!! Conversions from Thomas Klemm's HP-42S programs above are straightforward: Code:
Gerson. |
|||
01-22-2015, 08:41 AM
Post: #16
|
|||
|
|||
RE: Small RPN exponential routine
Thank you for the HP-41C Code. Now for a slightly more difficult request. How about a version fo rthe HP-67?
Namir |
|||
01-22-2015, 06:59 PM
Post: #17
|
|||
|
|||
RE: Small RPN exponential routine
(01-22-2015 08:41 AM)Namir Wrote: Thank you for the HP-41C Code. Now for a slightly more difficult request. How about a version fo rthe HP-67? No HP-67 here, but I have a printed manual. These should be equivalent to the above HP-41C programs and hopefully work on the HP-67: Code:
Gerson. |
|||
01-23-2015, 05:11 PM
Post: #18
|
|||
|
|||
RE: Small RPN exponential routine
(01-22-2015 06:59 PM)Gerson W. Barbosa Wrote:(01-22-2015 08:41 AM)Namir Wrote: Thank you for the HP-41C Code. Now for a slightly more difficult request. How about a version fo rthe HP-67? Gerson, Thank you so much for the HP-67 code. Have you considered attending an HHC conference in the US? It will be a special treat to meet you in person. I am sure you will have a time of your life being among peers who respect you and your contributions here. Namir |
|||
01-23-2015, 06:48 PM
Post: #19
|
|||
|
|||
RE: Small RPN exponential routine
(01-23-2015 05:11 PM)Namir Wrote: Gerson, Namir, Thank you very much for your kind words, despite my ever fading contributions. Yes, I've already considered attendind one HHC conference. It would be a pleasure meeting you all in person. Perhaps one of these years :-) Thanks again for the invitation! Gerson. |
|||
01-27-2015, 12:56 PM
Post: #20
|
|||
|
|||
RE: Small RPN exponential routine
Is this the origin for the LPN1 function? (I need to be here more often)
|
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 7 Guest(s)