Re: And, look over the sample 30b programs ... Re: 30b - Impressed Message #20 Posted by Thomas Klemm on 24 Aug 2010, 5:40 p.m., in response to message #19 by gene wright
Example 3: Base conversions
This program can be made somewhat shorter. I transliterated an old HP-15C (or whatever)
program to the new 30b syntax.
Here's the original:
001 - 42,21,11 LBL A
002 - 44 2 STO 2
003 - 34 x<>y
004 - 44 1 STO 1
005 - 30 -
006 - 34 x<>y
007 - 44 0 STO 0
008 - 42,21, 0 LBL 0
009 - 45 1 RCL 1
010 - 34 x<>y
011 - 45 2 RCL 2
012 - 10 ÷
013 - 43 44 INT
014 - 43 20 x=0
015 - 22 1 GTO 1
016 - 33 R-v
017 - 20 ×
018 - 33 R-v
019 - 20 ×
020 - 44,30, 0 STO - 0
021 - 33 R-v
022 - 22 0 GTO 0
023 - 42,21, 1 LBL 1
024 - 45 0 RCL 0
025 - 43 32 RTN
And here's the 30b version:
Step function comments
1 STO 2 Store the output base in memory 2.
2 ) Swap Performs a stack swap of the X and Y registers.
3 STO 1 Store the input base in memory 1.
4 - Difference of output base and input base.
5 x<>y Performs a stack swap of the X and Y registers.
6 STO 0 Store the number to convert in memory 0.
7 LBL 00 Label 00 is the main loop.
8 RCL 1 Recall input base.
9 ) Swap Performs a stack swap of the X and Y registers.
10 RCL 2 Recall output base.
11 / Divide number by output base.
12 Math Accesses the IP (integer part) function in the math menu.
13 Up Accesses the IP (integer part) function in the math menu.
14 Up Accesses the IP (integer part) function in the math menu.
15 Input Accesses the IP (integer part) function in the math menu.
16 Input Duplicates value to stack y since GF consumes stack x value.
17 GF 01 Exit loop if value is zero.
18 ( R-v This executes a roll down of the 4-level stack.
19 * Multiply weight by input base.
20 ( R-v This executes a roll down of the 4-level stack.
21 * Multiply dividend by weight.
22 STO - 0 Subtract from the number to convert.
23 ( R-v This executes a roll down of the 4-level stack.
24 Gto 00 Loop back to label 00.
25 LBL 01 Exit from the main loop ends here.
26 RCL 0 Recall final output number in new base.
27 Stop Inserts a Stop command. Program ends execution.
Unfortunately I can't test the program as don't own a 30b. Yet I hope
I didn't forget something this time.
The usage is the same as in the example:
Quote:
Convert 175 base 8 to base 10. Key in 175 = 8 = 10 = and run the program.
How does it work? The number is continuously divided by the output base
while the difference of input base and output base is multiplied by the input base.
The product of dividend and weight is added (or subtracted) from the original number.
175 = 175
17 * -2 = - 34
1 * -16 = - 16
-----
125
===
Quote:
175 base 8 is equal to 125 base 10. Now convert this result to base 2.
125 = 125
62 * 8 = + 496
31 * 80 = + 2480
15 * 800 = + 12000
7 * 8000 = + 56000
3 * 80000 = + 240000
1 * 800000 = + 800000
-------
1111101
=======
In the HP 30b programming example the following is used to get a digit:
Quote:
- Divide number to convert by output base.
- Accesses the FP (fractional part) function in the math menu.
- Multiply fractional part by output base to get digit.
I assume severe rounding errors may happen when trying to convert
big numbers and/or when one of the bases is not 2, 4, 5, 8 or 10.
On the HP 15C you get:
1,234,567,890 ENTER 7 / FRAC 7 * -> 2.8
While the correct answer for 1,234,567,890 MOD 7 is 3.
On the HP 30b you get probably a similar result.
Therefore I assume the following conversion will not work correctly when
the program from the HP 30b programming examples is used:
123,456,789(10) -> 3,026,236,221(7)
These kind of errors can't happen with the program
above as only integer values are used (given that only integers are entered).
Kind regards
Thomas
Addendum:
Quote:
Bases greater than 10 are not supported by this program.
True, but you may use the following trick to convert a decimal number to its hexadecimal presentation:
51966 = 100 = 16 =
Run the program to get: 12:10:15:14
This can easily be transliterated to: CAFE
Edited: 24 Aug 2010, 6:33 p.m.
|