(15C) Decimal to x/16
09-24-2015, 11:56 PM (This post was last modified: 06-15-2017 01:34 PM by Gene.)
Post: #1
 Dwight Sturrock Member Posts: 125 Joined: Dec 2013
(15C) Decimal to x/16
HP 15c x/16

Program to convert decimal fraction to a fraction readable on a tape measure:

Registers:
STO 0- ORIGINAL NUMBER
STO 1- NUMERATOR
STO 2- DENOMINATOR

Code:
01 LBL A 02 STO 0 03 FRAC      //Creates numerator x as a fraction              //with 16 as denominator: x/16 04 1         //Use 32 ON steps 04-05 if you want 32nds 05 6 06 STO 2 07 * 08 FIX 0 09 RND 10 FIX 3 11 INT 12 STO 1 13 LBL 7     //Can fraction be reduced? 14 2 15 / 16 FRAC 17 TEST 0     //Same as X#0 18 GTO 8     //If fraction can't be reduced,              //go to formatting subroutine 19 RCL 2     //Reduce fraction 20 2 21 / 22 STO 2 23 RCL 1 24 2 25 / 26 STO 1 27 GTO 7     //Try again 28 LBL 8     //Formatting subroutine 29 RCL 0     //Original number will be left in y register 30 RCL 1     //The next 7 steps put the fraction             //into the format Num.Deno: 31 RCL 2     //For Example, 1/16 will display             //as 1.16, 5/8 as 5.08 32 . 33 0 34 1 35 * 36 + 37 RTN
09-25-2015, 04:10 PM
Post: #2
 Thomas Klemm Senior Member Posts: 1,447 Joined: Dec 2013
RE: HP 15C: Decimal to x/16
That's a nice program. Here's a variant that is a little shorter and uses only one register:

Code:
001-42,21,11    LBL A 002-    44 0    STO 0 003-   42 44    FRAC 004-      48    . 005-       5    5 006-      36    ENTER 007-      36    ENTER 008-   43 33    R^ 009-       1    1 010-       6    6 011-      20    * 012-      40    + 013-   43 44    INT 014-      48    . 015-       1    1 016-       6    6 017-      40    + 018-42,21, 0    LBL 0 019-      20    * 020-   42 44    FRAC 021-43,30, 7    TEST 7 ; x>y 022-    22 1    GTO 1 023-   43 35    CLx 024-   43 36    LSTx 025-    22 0    GTO 0 026-42,21, 1    LBL 1 027-    45 0    RCL 0 028-   43 36    LSTx 029-      36    ENTER 030-      40    + 031-   43 32    RTN

The numerator and denominator are merged into a single number nn.dd. Thus we can divide both by 2 with a single instruction.
The display settings aren't changed since the function INT(x + 0.5) is used to round a number.
The stack is filled with 0.5 in lines 4-7 because this number can be used often.
Personally I dislike that 16 occurs twice but I couldn't come up with a better solution. Maybe somebody else can?

Kind regards
Thomas
09-26-2015, 02:11 PM
Post: #3
 Dwight Sturrock Member Posts: 125 Joined: Dec 2013
RE: HP 15C: Decimal to x/16
Elegant, Thomas, and a bit faster.

I had a version for the 42S and I ported it quickly to the 15C years ago without much thought. It is a program I used on a daily basis in my work for many years.
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)