HP-67 Base Conversion from HP Key Notes
|
10-19-2015, 11:31 AM
Post: #1
|
|||
|
|||
HP-67 Base Conversion from HP Key Notes
Dear All,
in HP Key Notes Vol. 3 No. 2 p. 13 there was a base conversion routine for the HP-67/97 written by Cass R. Lewart (Holmdel, New Jersey). In the following issue (Vol. 3 No. 3 p. 11) Bob Edelen corrected a missing program step in this routine. The program works perfectly and even can convert directly from one non-decimal base to another non-decimal base (e.g. from base 2 to base 8) just by entering: number ENTER original base ENTER new base Function key A The program listing is as follows: 001 *LBL A 002 STO 0 003 Roll Down 004 Enter 005 GSB 3 006 GSB 0 007 RCL 0 008 GSB 3 009 RCL 0 010 *LBL 0 011 STO 1 012 Roll Down 013 STO 2 014 Roll Down 015 STO 3 016 0 017 STO 4 018 1 019 STO 5 020 *LBL 1 021 RCL 3 022 X=0? 023 GTO 2 024 RCL 1 025 / 026 ENTER 027 INT 028 STO 3 029 - 030 RCL 1 031 x 032 RCL 5 033 x 034 INT 035 ST+4 036 RCL 2 037 STx5 038 GTO 1 039 *LBL 2 040 RCL 4 041 RTN 042 *LBL 3 043 1 044 - 045 LOG 046 INT 047 1 048 + 049 10^x 050 RTN Alas, the original Key Notes article has not a tiny clue of how this routine works. And although I can see all the program steps I cannot reverse engineer the program to find out the algorithm which is behind this clever routine. I already did a Google search for base conversion algorithms but I only found descriptions for converting base X to base 10 and base 10 to base X. Most of them use string operations to seperate single digits. So my question is: Can anyone of you math gurus of this forum please explain the algorithm of this base conversion routine to me? Any hint will be very much appreciated. Thank you very much in advance. Best regards Karl |
|||
10-19-2015, 02:57 PM
(This post was last modified: 10-19-2015 06:35 PM by Thomas Klemm.)
Post: #2
|
|||
|
|||
RE: HP-67 Base Conversion from HP Key Notes
(10-19-2015 11:31 AM)Karl-Ludwig Butte Wrote: So my question is: Can anyone of you math gurus of this forum please explain the algorithm of this base conversion routine to me? Here are some comments to the code: Code: 001 *LBL A Thus the number is first translated from the original base to a base of \(10^m\) and then from this to the new base. These programs for the Base Conversion do a similar thing but only halfway. It's mostly only what is done in the sub-routine 0. However the algorithm used in this program could be improved a little with the following trick. Say you want to translate 23 to binary: \[ \begin{align} 23 \div 2 &= 11 & \, (1) \\ 11 \div 2 &= 5 & \, (1) \\ 5 \div 2 &= 2 & \, (1) \\ 2 \div 2 &= 1 & \, (0) \\ 1 \div 2 &= 0 & \, (1) \\ \end{align} \] The remainders are then multiplied by powers of 10 and added: \[ \begin{align} 1 \times 10^0 &= 1 \\ 1 \times 10^1 &= 10 \\ 1 \times 10^2 &= 100 \\ 0 \times 10^3 &= 0\\ 1 \times 10^4 &= 10000 \\ &= 10111 \end{align} \] We can write the remainders using the first equations: \[ \begin{align} 1 &= 23 - 2 \times 11 \\ 1 &= 11 - 2 \times 5 \\ 1 &= 5 - 2 \times 2 \\ 0 &= 2 - 2 \times 1 \\ 1 &= 1 - 2 \times 0 \\ \end{align} \] We do the same multiplication by powers of 10: \[ \begin{align} 1 \times 10^0 &= 23 \times 10^0 - 2 \times 11 \times 10^0 \\ 1 \times 10^1 &= 11 \times 10^1 - 2 \times 5 \times 10^1 \\ 1 \times 10^2 &= 5 \times 10^2 - 2 \times 2 \times 10^2 \\ 0 \times 10^3 &= 2 \times 10^3 - 2 \times 1 \times 10^3 \\ 1 \times 10^4 &= 1 \times 10^4 - 2 \times 0 \times 10^4 \\ \end{align} \] Now we can see that the same numbers appear on different lines. Thus we can re-group the sum: \[ \begin{align} & 23 \times 10^0 \\ + & 11 \times (10^1 - 2 \times 10^0) \\ + & 5 \times (10^2 - 2 \times 10^1) \\ + & 2 \times 10^3 - 2 \times 10^2) \\ + & 1 \times 10^4 - 2 \times 10^3) \\ \end{align} \] We end up with: \[ \begin{align} & 23 \\ + & 11 \times 8 \\ + & 5 \times 80 \\ + & 2 \times 800 \\ + & 1 \times 8000 \\ \end{align} \] Thus we can avoid calculating the remainders. We just need to calculate the sequence 23, 11, 5, 2, 1 and multiply by the correct factors. Kind regards Thomas |
|||
10-19-2015, 04:08 PM
Post: #3
|
|||
|
|||
RE: HP-67 Base Conversion from HP Key Notes
Here's the version of the program that implements the trick mentioned in my previous post:
Code: 001 *LBL A Example: 3258 to base 3 325 ENTER 8 ENTER 3 A 21220 I tested the original program using an HP-11C and got 20219. This is probably due to this line: Code: 034 INT After changing it to RND (rounding) I got the correct result. The program above isn't affected by this rounding problem. Cheers Thomas |
|||
10-19-2015, 06:32 PM
Post: #4
|
|||
|
|||
RE: HP-67 Base Conversion from HP Key Notes
This variant uses some stackrobatic so we need only 4 registers instead of 6:
Code: 001 *LBL A And it's even a little shorter. |
|||
10-19-2015, 07:35 PM
Post: #5
|
|||
|
|||
RE: HP-67 Base Conversion from HP Key Notes
Hi Thomas,
thanks a lot for your three posts, explaining the algorithm and optimizing the code - great work :-) Best regards Karl |
|||
10-19-2015, 09:06 PM
Post: #6
|
|||
|
|||
RE: HP-67 Base Conversion from HP Key Notes
While I was running errands it occurred to me that perhaps someone could enter the program on an HP97 and run it with a few examples using 'TRACE' mode.
I've returned and see the problem is already taken care of. You've all done very well. 2speed HP41CX,int2XMEM+ZEN, HPIL+DEVEL, HPIL+X/IO, I/R, 82143, 82163, 82162 -25,35,45,55,65,67,70,80 |
|||
10-19-2015, 09:34 PM
Post: #7
|
|||
|
|||
RE: HP-67 Base Conversion from HP Key Notes
There's another program Base Conversions for the HP-67 in the Old Software Library which was originally published in the HP-67 Math Pac 1.
It can even find the binary representation of \(\pi\)! Should we still add the optimized program to the new HP-65/67/97 Software Library so it may be found easier? Cheers Thomas |
|||
10-19-2015, 10:34 PM
(This post was last modified: 10-19-2015 10:36 PM by Thomas Klemm.)
Post: #8
|
|||
|
|||
RE: HP-67 Base Conversion from HP Key Notes
(10-19-2015 09:06 PM)TASP Wrote: (...) perhaps someone could enter the program on an HP97 and run it with a few examples using 'TRACE' mode. Not exactly what you want but I quickly wrote a convert(n_from, from_base, to_base) function that let's you run the innermost loop. Thus either from_base or to_base must be a power of 10. Here are the examples I used: convert(23, 10, 2) Code: Code | X | Y | Z | T | Register convert(325, 8, 10) Code: Code | X | Y | Z | T | Register convert(213, 10, 3) Code: Code | X | Y | Z | T | Register You can use it with your own examples if you want. Just follow the link above and click run. Enter the convert function with the desired parameters into the black box and hit the Enter key. Cheers Thomas PS: I cheated a little with this line: Code: 021 / |
|||
10-20-2015, 07:48 PM
Post: #9
|
|||
|
|||
RE: HP-67 Base Conversion from HP Key Notes
Hi Thomas,
(10-19-2015 09:34 PM)Thomas Klemm Wrote: Should we still add the optimized program to the new HP-65/67/97 Software Library so it may be found easier? That's an excellent idea. Thanks again for your superb answers. Best regards Karl |
|||
10-21-2015, 09:10 PM
Post: #10
|
|||
|
|||
RE: HP-67 Base Conversion from HP Key Notes | |||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)