RatNums.mesa
Last Edited by: Arnon, June 10, 1985 4:19:22 pm PDT
DIRECTORY
Rope USING [ROPE],
IO USING [STREAM, PutRope],
BigCardinals;
RatNums: CEDAR DEFINITIONS
IMPORTS IO =
BEGIN OPEN BC: BigCardinals;
***** Data Structure *****
Sign: TYPE = { NEGATIVE, ZERO, POSITIVE };
RatNum: TYPE = RECORD [sign: Sign, numerator, denominator: BC.BigCARD ];
RatNum's equal to zero are expected to have sign ZER0 and zero numerator; their denominators are undefined.
***** Basic Arithmetic *****
RatNumAdd: PROC [in1, in2: RatNum] RETURNS [out: RatNum];
RatNumNegate: PROC [in: RatNum] RETURNS [out: RatNum];
RatNumABS: PROC [in: RatNum] RETURNS [out: RatNum];
RatNumSubtract: PROC [in1, in2: RatNum] RETURNS [RatNum];
RatNumMultiply: PROC [in1, in2: RatNum] RETURNS [out: RatNum];
RatNumInverse: PROC [in: RatNum] RETURNS [out: RatNum];
Inverse of a non-zero RatNum.
RatNumDivide: PROC [dividend, divisor: RatNum] RETURNS [RatNum];
***** Constructor Routines *****
RatNumFromBigCards: PROC [sign: INTEGER[-1..1], num, den: BC.BigCARD] RETURNS [out: RatNum];
RatNumFromSmallCards: PROC [sign: INTEGER[-1..1], num, den: CARDINAL] RETURNS [out: RatNum];
MakeRatNumZero: PROC RETURNS [out: RatNum];
Construct RatNum representation for zero
***** Conversion and I/O Routines *****
RatNumFromREALRope: PROC [in: Rope.ROPE] RETURNS [RatNum];
Intended to allow decimal notation input of RatNums.
RatNumToRatRope: PROC [in: RatNum, reuse: BOOLEANFALSE, showDenomOne: BOOLFALSE] RETURNS [Rope.ROPE];
Used for displaying the values of RatNums. If in is positive, then the output Rope is unsigned. The default of reuse: FALSE preserves the input RatNum. If in is an integer, then if showDenomOne = FALSE the denominator of one is not printed, if TRUE then it is.
RatNumFromREAL: PROC [in: REAL] RETURNS [RatNum];
Convert a REAL to a RatNum. The REAL is viewed as a decimal fraction, whose numerator and denominator become the numerator and denominator of the resulting RatNum.
RatNumToREAL: PROC [in: RatNum, reuse: BOOLEANFALSE] RETURNS [REAL];
Convert a RatNum to a real. For the time being, it is assumed that the RatNum is not so large in absolute value as to cause floating point overflow.
ReadRatNum: PROC [in: IO.STREAM] RETURNS [out: RatNum, ok: BOOLTRUE];
WriteRatNum: PROC [stream: IO.STREAM, in: RatNum] = INLINE {
IO.PutRope[ stream, RatNumToRatRope[in] ]
};
***** Comparison *****
RatNumRelation: TYPE = {ratLess, ratEqual, ratGreater};
RatNumCompare: PROC [in1, in2: RatNum] RETURNS [RatNumRelation];
RatNumRelationToRope: PROC [inRelation: RatNumRelation] RETURNS[Rope.ROPE];
RatNumGreater: PROC [in1, in2: RatNum] RETURNS [BOOLEAN];
RatNumEqual: PROC [in1, in2: RatNum] RETURNS [BOOLEAN];
RatNumLess: PROC [in1, in2: RatNum] RETURNS [BOOLEAN];
RatNumPositive: PROC [in: RatNum] RETURNS [BOOLEAN];
Test if a RatNum is zero
RatNumZero: PROC [in: RatNum] RETURNS [BOOLEAN];
Test if a RatNum is zero
RatNumNegative: PROC [in: RatNum] RETURNS [BOOLEAN];
Test if a RatNum is zero
RatNumInteger: PROC [in: RatNum] RETURNS [BOOLEAN];
Test if a RatNum is an integer
***** Miscellaneous BigCardinal routines *****
BigPowerOfTen: PROC [exponent: CARDINAL] RETURNS [power: BC.BigCARD];
BigToREAL: PROC [in: BC.BigCARD, reuse: BOOLEANFALSE] RETURNS [out: REAL];
BigOne: PROC [in: BC.BigCARD] RETURNS [BOOLEAN];
Test if a BigCARD is the integer one.
END.