Task

Create a reasonably complete implementation of rational arithmetic in the particular language using the idioms of the language.



Example

Define a new type called frac with binary operator "//" of two integers that returns a structure made up of the numerator and the denominator (as per a rational number).

Further define the appropriate rational unary operators abs and '-', with the binary operators for addition '+', subtraction '-', multiplication '×', division '/', integer division '÷', modulo division, the comparison operators (e.g. '<', '≤', '>', & '≥') and equality operators (e.g. '=' & '≠').

Define standard coercion operators for casting int to frac etc.

If space allows, define standard increment and decrement operators (e.g. '+:=' & '-:=' etc.).

Finally test the operators:
Use the new type frac to find all perfect numbers less than 219 by summing the reciprocal of the factors.



Related task

   Perfect Numbers


DEFINITION MODULE Rational;
    TYPE RAT =  RECORD
                    numerator : INTEGER;
                    denominator : INTEGER;
                END;
 
    PROCEDURE IGCD( i : INTEGER; j : INTEGER ) : INTEGER;
    PROCEDURE ILCM( i : INTEGER; j : INTEGER ) : INTEGER;
    PROCEDURE IABS( i : INTEGER ) : INTEGER;
 
    PROCEDURE RNormalize( i : RAT ) : RAT;
    PROCEDURE RCreate( num : INTEGER; dem : INTEGER ) : RAT;
    PROCEDURE RAdd( i : RAT; j : RAT ) : RAT;
    PROCEDURE RSubtract( i : RAT; j : RAT ) : RAT;
    PROCEDURE RMultiply( i : RAT; j : RAT ) : RAT;
    PROCEDURE RDivide( i : RAT; j : RAT ) : RAT;
    PROCEDURE RAbs( i : RAT ) : RAT;
    PROCEDURE RInv( i : RAT ) : RAT;
    PROCEDURE RNeg( i : RAT ) : RAT;
 
    PROCEDURE RInc( i : RAT ) : RAT;
    PROCEDURE RDec( i : RAT ) : RAT;
 
    PROCEDURE REQ( i : RAT; j : RAT ) : BOOLEAN;
    PROCEDURE RNE( i : RAT; j : RAT ) : BOOLEAN;
    PROCEDURE RLT( i : RAT; j : RAT ) : BOOLEAN;
    PROCEDURE RLE( i : RAT; j : RAT ) : BOOLEAN;
    PROCEDURE RGT( i : RAT; j : RAT ) : BOOLEAN;
    PROCEDURE RGE( i : RAT; j : RAT ) : BOOLEAN;
 
    PROCEDURE RIsZero( i : RAT ) : BOOLEAN;
    PROCEDURE RIsNegative( i : RAT ) : BOOLEAN;
    PROCEDURE RIsPositive( i : RAT ) : BOOLEAN;
 
    PROCEDURE RToString( i : RAT; VAR S : ARRAY OF CHAR );
    PROCEDURE RToRational( s : ARRAY OF CHAR ) : RAT;
 
    PROCEDURE WriteRational( i : RAT );
 
END Rational.