
   These 9 programs will allow you to +,-,/,* fractions, and
convert fractions <--> decimal numbers.
   The first menu row should show { ADD Sub MUL DIV CONV SIMP }
FMAT, IMP, and SIM are "support files" for the other programs
and you will not need to see them.  The 9 programs take up 1713 bytes.
   I have found it is easiest to enter numbers like "{1,2,3[ENTER]"
and the HP will convert that to { 1 2 3 }.  You could also enter
fractions like "1,2,3 DIV ADD" so you can use it in one-hand mode.
   Beware of negative numbers, some calculations will work but
others will go on calculating forever.
   I did borrow some code for the CONV program to convert a
decimal number to a fraction from jhc@Crnlvax5.BITNET (James
H. Cloos, Jr.).
   CONV will take 2 or 1 arguments depending on what you want to do.
giving a number in { } will cause it to give the decimal value.
Giving it a number with a decimal point with an optional number of
digits of accuracy will convert it into { A B C } format.
   SIMP takes a number like { 455 10 } and reduces it to { 45 5 10 }.
SIMP calls SIM which take { 45 5 10 } and further reduces it to
{ 45 1 2 }.  SIM has a small problem, it is kind of slow.  Ok, it
is REALLY slow for big numbers. 8 seconds for { 2 400 } and 1
minute for { 2 4000 }.  Feel free to re-write SIM and post this
entire file with your modification.  (Just E-Mail me a copy
so I can use the improved version)
   Please contact me if you have any bugs/problems/comments/ideas...
Ian Matthew Smith <IMS103@PSUVM.BITNET>

Listing Conventions:      >=  Shift [T] Greater than or equal to.
                          ->  Shift [U]
                          << and >> are the key next to [SPACE]

       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 }
123.456 CONV = { 7037 57 } SIMP = { 123 26 57 }
{ 1 3 4 } CONV = 1.75

3.14259265359 6 CONV = { 355 113 } SIMP = { 3 16 113 }
              |
              +------- accuracy


{ CHK }  CHK:C624
<<
   RCLF STD HEX 16 STWS SWAP RCL ->STR DUP #0h 1 ROT SIZE
   FOR j
      OVER j j SUB NUM R->B XOR RL
   NEXT
   ->STR 3 OVER SIZE 1 - SUB ROT STOF SWAP DROP
>>

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

{ Sub }  CHK:FFB9
<<
   IMP LIST-> DROP 3 ROLL IMP LIST-> DROP -> c d a b
   <<
      0 a d * b c - b d * 3 ->LIST SIMP
   >>
>>

{ MUL }  CHK:FA40
<<
   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 }  CHK:DA1D
<<
   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
   >>
>>

{ CONV }  CHK:731D
<<
   IF DUP TYPE 5 == THEN
      FMAT LIST-> DROP / +
   ELSE
      IF DUP FP 0 > THEN
         DUP FP ->STR SIZE 1 -
      END
      NEG ALOG .5 * 1 0 -> x p a b                   --+
      <<                                               | I took this
         x                                             | code segment
         WHILE 'ABS(FLOOR(x*a+.5)-x*a)>=p*a' EVAL      | from an article
         REPEAT                                        | posted by
           INV DUP IP ABS a * b + a 'b' STO 'a' STO FP | jhc@Crnlvax5.BITNET
         END                                           | in comp.sys.handhelds
         DROP 'FLOOR(x*a+.5)' EVAL a                 --+
         { } + +
      >>
   END
>>

{ SIMP }  CHK:AAC2
<<
   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
   >>
>>

{ IMP }  CHK:8A21
<<
   FMAT LIST-> DROP -> a b c
   <<
      a c * b + c 2 ->LIST
   >>
>>

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

{ SIM }  CHK:BAE2
<<
   LIST-> DROP 2 0 0 -> a b c j k l
   <<
      DO
         c 'l' STO
         DO
            b j MOD c j MOD +
            IF NOT THEN
               b j / 'b' STO c j / 'c' STO c 'j' STO 1 'k' STO
            END
            j 1 + 'j' STO j 2 *
         UNTIL c > END
         a b c 3 ->LIST
         IF k 0 > THEN
            SIM
         END
      UNTIL c k >= END
   >>
>>

Ian Matthew Smith <IMS103@PSUVM.BITNET>
