Power-of-two fraction handling for the 41C
|
01-31-2016, 09:45 PM
Post: #1
|
|||
|
|||
Power-of-two fraction handling for the 41C
I was wondering if there is a program available that converts from decimal to fractions but limits itself to a user-defined power of two in the denominator. For example, if my power-of-two limit is 32 and I have a decimal number of 1.3525, then my conversion would be 1-11/32. Likewise, if my denominator limit is 64, the answer would be 1-23/64. The smallest denominator should always be used; for example, if the decimal number is 1.25, the answer would be 1-1/4, even if the denominator limit is 8 or higher. An added useful feature would be always representing the denominator in the given power of two upon setting of a flag; for example FS 04, then 1.25 = 1-16/64.
Does such a program exist, or should I write this? David Brunell Houston, Texas |
|||
01-31-2016, 11:24 PM
(This post was last modified: 01-31-2016 11:25 PM by Dieter.)
Post: #2
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
(01-31-2016 09:45 PM)quantalume Wrote: I was wondering if there is a program available that converts from decimal to fractions but limits itself to a user-defined power of two in the denominator That's easy – if the denominator is a power of 2 (or another base) all factors of this are 2 as well. So you could use the respective setting of calculators with fraction function, for instance the 35s. As far as the 41-series is concerned... (01-31-2016 09:45 PM)quantalume Wrote: Does such a program exist, or should I write this? Yes, it exists – I just wrote one. ;-) Yes, you should write your own as well – it's fun. 32 [STO] 00 1,3525 [R/S] 1 11/32 [<–] 1,3438 64 [STO] 00 1,3525 [R/S] 1 23/64 [<–] 1,3594 1,25 [R/S] 1 1/4 [<–] 1,2500 [SF] 04 1,25 [R/S] 1 16/64 [<–] 1,2500 Dieter |
|||
02-01-2016, 01:23 AM
Post: #3
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C | |||
02-01-2016, 02:06 AM
Post: #4
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
(01-31-2016 11:24 PM)Dieter Wrote:(01-31-2016 09:45 PM)quantalume Wrote: Does such a program exist, or should I write this? OK, I shall then. We can compare code when I finish. David Brunell Houston, Texas |
|||
02-01-2016, 02:10 AM
Post: #5
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
(02-01-2016 01:23 AM)Dwight Sturrock Wrote: http://www.hpmuseum.org/software/42decinc.htm That's a good start. I also looked at the AECROM unit-of-length feature. They are both limited to 1/16 as the finest division, however. David Brunell Houston, Texas |
|||
02-01-2016, 07:10 AM
(This post was last modified: 02-01-2016 07:12 AM by Dieter.)
Post: #6
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
(02-01-2016 02:06 AM)quantalume Wrote: OK, I shall then. We can compare code when I finish. Fine. Here's my version: Code: 01 LBL "D2F" Usage: Store the maximum denominator in R00. No other data registers are used. Set flag 04 if you want a fixed denominator, otherwise the fraction with the minimum denominator is returned. 64 STO 00 1,3525 XEQ"D2F" => 1 23/64 The rounded result is returned in X, the other stack registers hold the three values of the result. T: 1 Z: 23 Y: 64 X: 1,359375 Variations: The program can be easily modified to accept the max. denominator on the stack (fraction ENTER denominator XEQ"D2F") and/or return improper fractions (87/64 in this case). Dieter |
|||
02-01-2016, 05:43 PM
(This post was last modified: 02-01-2016 06:18 PM by quantalume.)
Post: #7
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
OK, here is what I came up with. I avoided looking at your solution before I was finished.
Code: 01 LBL "TOF" I like your solution better as it is shorter, doesn't use an extra register and leaves the stack in a more-desirable state. You also take advantage of flag 29 to eliminate the decimal mark from the result. The only thing nice to add would be saving and recalling flag register d in order to return the calculator to the original mode setting, rather than assuming it was in FIX 4. David Brunell Houston, Texas |
|||
02-01-2016, 06:15 PM
(This post was last modified: 02-01-2016 06:18 PM by quantalume.)
Post: #8
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
I like to avoid dependencies, even on the CX function set. Trouble is, I'm finding it difficult to include a RCL d/STO d in Dieter's program without making it messy or disturbing the final stack.
David Brunell Houston, Texas |
|||
02-01-2016, 06:47 PM
Post: #9
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
Well, it would be easy enough to put a RCL d, STO 01 at the start of the program. However, by doing a RCL 01, STO d after the last ARCL, I lose one of the results off the top of the stack that Dieter is returning. It could be done, but it messes up his elegant program.
David Brunell Houston, Texas |
|||
02-01-2016, 08:43 PM
Post: #10
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
I took Dieter's code and added a check for the trivial case of calling the function with an integer. It now returns just the integer rather than the integer + "0/1". I won't bother saving/recalling flags as I keep the calculator in FIX 4 most of the time anyway. Hopefully Dieter will be along soon to clean up the mess I made of his code. It now has too many labels and jumps for my liking.
Code: 01 LBL "D2F" David Brunell Houston, Texas |
|||
02-01-2016, 09:10 PM
(This post was last modified: 02-01-2016 09:38 PM by Dieter.)
Post: #11
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
(02-01-2016 06:47 PM)quantalume Wrote: Well, it would be easy enough to put a RCL d, STO 01 at the start of the program. However, by doing a RCL 01, STO d after the last ARCL, I lose one of the results off the top of the stack that Dieter is returning. It could be done, but it messes up his elegant program. The problem is not the messed up stack – this can be avoided by using X<> commands. The problem is that both RCL and X<> normalize numbers, so that the original content of register d that was saved here, is altered before it can be stored back in d. This does not happen on the stack or in status registers, but the complete stack is used by the program. Hmmm... there might be a way. The program could set/clear flags 0...3 so that the first nibble of d becomes 1001, indicating this is an alpha string. Which might avoid the normalisation procedure. Maybe the experts can say more here. Another option is changing the program so that it uses one additional data register while d is kept on the stack. I think this should work. Anyway, I just added support for improper fractions. Since the program so far does not handle negative input, an ABS at the start also makes sense (just to be sure...). Dieter |
|||
02-01-2016, 09:36 PM
(This post was last modified: 02-01-2016 09:41 PM by Dieter.)
Post: #12
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
(02-01-2016 08:43 PM)quantalume Wrote: I took Dieter's code and added a check for the trivial case of calling the function with an integer. It now returns just the integer rather than the integer + "0/1". That's why I prefer flags here. Just clear or set e.g. flag 05 if MOD yields zero and skip the x/y output routine. (02-01-2016 08:43 PM)quantalume Wrote: I won't bother saving/recalling flags as I keep the calculator in FIX 4 most of the time anyway. Great – this avoids several problems I mentioned in my other post. Which does not mean it cannot be done... ;-) Here's a new version that uses flag 02 to switch between constant or minimized denominator, and flag 01 toggles between proper and improper fractions (in this case integer input is returned as x/1 instead of just x). Code: 01 LBL "D2F" And maybe we can also get this RCL d thing working... ;-) Dieter |
|||
02-01-2016, 11:24 PM
Post: #13
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
(02-01-2016 09:10 PM)Dieter Wrote:(02-01-2016 06:47 PM)quantalume Wrote: Well, it would be easy enough to put a RCL d, STO 01 at the start of the program. However, by doing a RCL 01, STO d after the last ARCL, I lose one of the results off the top of the stack that Dieter is returning. It could be done, but it messes up his elegant program. Ok, I was operating under the assumption that the register only becomes normalized when you try to use it in an arithmetic operation. The PPC ROM has a couple of functions for saving and restoring display mode, but unfortunately they utilize the alpha register. Seems like the best bet is to keep the flags on the stack. David Brunell Houston, Texas |
|||
02-02-2016, 08:21 PM
(This post was last modified: 02-02-2016 08:27 PM by Dieter.)
Post: #14
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
(02-01-2016 09:36 PM)Dieter Wrote: And maybe we can also get this RCL d thing working... ;-) Here is a version that adds some benefits:
Code: 01 LBL"D2F" Here are some examples: Code: 64 [STO] 00 Note: in improper fraction mode integer results are returned as x/1. Now try this version and see what you get. There may be errors, so as usual all remarks and error reports are welcome. Dieter |
|||
02-02-2016, 09:35 PM
(This post was last modified: 02-02-2016 09:37 PM by quantalume.)
Post: #15
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
(02-02-2016 08:21 PM)Dieter Wrote:(02-01-2016 09:36 PM)Dieter Wrote: And maybe we can also get this RCL d thing working... ;-) Wow, this is really nice! I have not been able to find any errors in testing. I did make one small change. So as not to display the sign twice for negative output expressed as mixed numbers (this is just a personal preference), I replaced lines 50 and 51 with: Code: 50 FC? 05 I appreciate the work you put into this, and I will definitely get daily use out of it. You should create an article in the 41C software library section once you're happy with the program. David Brunell Houston, Texas |
|||
02-02-2016, 11:25 PM
(This post was last modified: 02-02-2016 11:51 PM by Dieter.)
Post: #16
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
(02-02-2016 09:35 PM)quantalume Wrote: Wow, this is really nice! I have not been able to find any errors in testing. I did make one small change. So as not to display the sign twice for negative output expressed as mixed numbers (this is just a personal preference), I replaced lines 50 and 51 with: Logically this tests if both flag flag 05 and flag 01 are set. This can be accomplished without labels in a composite test, the last one returning "always false": Code: FS? 05 BUT... this messes up the stack output. The idea behind the double sign was that the value of the rounded fraction should equal T + Z/Y, and this is returned in X. Try it for negative input, and you'll see it doesn't work. I think the following version works better and the result should be displayed the way you prefer. It's even one line shorter. Code: 01 LBL"D2F" As usual: try it and see if it works for you. All error reports are welcome. Dieter |
|||
06-02-2016, 03:54 PM
(This post was last modified: 06-04-2016 01:19 PM by 4ster.)
Post: #17
|
|||
|
|||
RE: Power-of-two fraction handling for the 41C
I really like Dieter's first version (below) since it is compact and I don't mind the occasional "0/1" fraction portion of the answer. After having it on my calculator for a while I bought a "Thermal & Transport Science Pac" module. An unexpected (for me) feature of the module is that it has a fairly powerful unit management system that can be incorporated into user programs. After playing with it a while it dawned on me that with some simple additions to Dieter's program I could do full, fractional conversions to imperial unit systems.
See the "Thermal & Transport Science Pac" manual for details on how the conversion system works. Conversion examples, entered in the alpha register before running D2F: CM-IN (centimeters to inches with 1/8, 1/16, 1/32... precision by storing 8, 16, 32... in R00) M-YD (meters to yards and feet, by storing 3 in R00) KG-LBM (kilogram to pounds and ounces of mass by storing 16 in R00) I made the following additions to Dieter's program: At line 02, 5 lines are inserted: FS? 01 EXQ 03 (If flag 01 is set a unit conversion is run on the input before being fractionalized) FS? 55 VIEW X (if a printer is present it prints the converted, digital, input) ASTO 01 (stores unit conversion string to put back into the alpha register at the program's end) At line 27 insert: CLA (to clear the alpha register of the conversion string) At line 40 insert: CLA (clears the alpha register of the fractionalized answer) ARCL 01 (Puts the unit conversion string back into alpha so its ready for another input.) GTO 04 LBL 03 VIEW X (prints the input) AVIEW (prints the conversion type to be be run) -SI (The unit conversion sub-routine on the Thermal module) RTN LBL 04 END Usage: If I want to run the unit conversion before fractionalizing the number I enter the unit conversion string into alpha and set flag 01. Note that since register 1 is temporarily used to store the alpha string of the unit conversion, that string is limited to 6 characters. KG-LBM is fine, KGF-LBF (kilogram force to pound force) is seven characters so will not work. Most of the valid conversions strings are 6 characters or less. If a printer is attached, the program prints four items: the original input, the conversion string used, the conversion, and the fractionalized conversion. (02-01-2016 07:10 AM)Dieter Wrote:(02-01-2016 02:06 AM)quantalume Wrote: OK, I shall then. We can compare code when I finish. Steve In order of appearance: HP 41CV, CMT-MCGPS, HP 41CX, DM 41, DM 42 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)