[comp.sys.hp] HP-28S Fractions program

IMS103@PSUVM.BITNET (10/14/89)

I recently typed in all my important HP-28S programs just in case
I messed up replacing the batteries.  Here is a set of programs
that will enable you to add, subtract, multiply, and divide using
fractions.

The first menu row should show { ADD SUB MUL DIV F-R SIMP }
FMAT, IMP, and SIM are "support files" for the other programs
and you will not need to see them.
Beware of negative numbers, some calculations will work but
others will go on calculating forever.

      B
   A ---  =  { A B C }
      C

Examples...

17 5 DIV = { 3 2 5 }
{ 1 2 } 4 MUL = 2
{ 1 2 3 } { 4 5 6 } ADD = { 6 1 2 }
{ 5 1 2 } { 3 4 } SUB = { 4 3 4 }
{ 5 20 40 } SIMP = { 5 1 2 }
{ 87 4 } SIMP { 21 3 4 }
{ 1 3 4 } F->R 1.75

{ ADD }
     <<
        IMP LIST-> DROP 3 ROLL IMP LIST-> DROP -> c d a b
        <<
           0 a d * b c + b d * 3 ->LIST SIMP
        >>
     >>

{ SUB }
     <<
        IMP LIST-> DROP 3 ROLL IMP LIST-> DROP -> c d a b
        <<
           0 a d * b c - b d * 3 ->LIST SIMP
        >>
     >>

{ MUL }
     <<
        FMAT SWAP FMAT LIST-> DROP 4 ROLL LIST-> DROP -> a b c d e f
        <<
           0 a c * b + d f * e + * f c * 3 ->LIST SIMP
        >>
     >>

{ DIV }
     <<
        FMAT SWAP FMAT LIST-> DROP 4 ROLL LIST-> DROP -> a b c d e f
        <<
           0 a c * b + f * f d * e + c * 3 ->LIST SIMP
        >>
     >>

{ SIMP }
     <<
        FMAT LIST-> DROP -> a b c
        <<
           IF b c >= THEN
              a b c / IP + b b c / IP c * - c
           ELSE
              a b c
           END
           3 ->LIST SIM DUP LIST-> DROP DROP +
           IF NOT THEN
              DROP 0
           ELSE
              DUP 1 GET
              IF NOT   THEN
                 LIST-> 1 - ->LIST SWAP DROP
              END
              DUP 2 GET
              IF NOT THEN
                 LIST-> DROP DROP DROP
              END
           END
        >>
     >>

{ F->R }
     <<
        FMAT LIST-> DROP / +
     >>

{ IMP }
     <<
        FMAT LIST-> DROP -> a b c
        <<
           a c * b + c 2 ->LIST
        >>
     >>

{ FMAT }
     <<
        DUP TYPE
        IF NOT THEN
           { } + 0 + 1 +
        ELSE
           DUP SIZE
           IF 3 < THEN
              0 SWAP +
           END
        END
     >>

{ SIM }
     <<
        LIST-> DROP 2 0 0 -> a b c d e f
        <<
           DO
              c 'f' STO
              DO
                 b d MOD c d MOD +
                 IF NOT THEN
                    b d / 'b' STO c d / 'c' STO c 'd' STO 1 'e' STO
                 END
                 d 1 + 'd' STO d 2 *
              UNTIL c > END
              a b c 3 ->LIST
              IF e 0 > THEN
                 SIM
              END
           UNTIL c e >= END
        >>
     >>