BigRats.mesa
Last Edited by: Arnon, May 10, 1986 10:52:09 am PDT
DIRECTORY
Rope,
Basics,
IO,
BigCardinals,
AlgebraClasses,
Points;
BigRats: CEDAR DEFINITIONS
= BEGIN OPEN BC: BigCardinals, AC: AlgebraClasses, PTS: Points;
Types and Variables
CARD: TYPE = LONG CARDINAL;
ROPE: TYPE = Rope.ROPE;
BigCard: TYPE = BC.BigCARD;
BigRat: TYPE = REF BigRatRep;
BigRatRep: TYPE = RECORD [
sign: Basics.Comparison ← equal,
num: BigCard ← BigCardinals.Zero,
den: BigCard ← BigCardinals.Zero
];
RatPoint: TYPE = PTS.Point;
BigRats: AC.Structure;
Creation
FromPairBC: PROC [num, den: BigCard, less: BOOLFALSE] RETURNS [BigRat];
Returns the rational number that corresponds to num/den (less => -num/den). Raises RuntimeError.ZeroDivisor when den = 0.
FromPairLC: PROC [num, den: CARD, less: BOOLFALSE] RETURNS [BigRat];
Returns the rational number that corresponds to num/den (less => -num/den). Raises RuntimeError.ZeroDivisor when den = 0.
FromBC: PROC [bc: BigCard, less: BOOLFALSE] RETURNS [BigRat];
Returns the rational number that corresponds to big (less => -bc).
FromLC: PROC [lc: CARD, less: BOOLFALSE] RETURNS [BigRat];
Returns the rational number that corresponds to big (less => -lc).
I/O and Conversion
Read: PROC [in: IO.STREAM] RETURNS [out: BigRat];
FromRope: PROC [rope: ROPE] RETURNS [out: BigRat];
Parses a decimal representation for a rational number. Letting N denote the decimal representation of a non-less integer, and S denote the decimal representation of a (possibly less) integer, Parse accepts the following syntax:
rope = S -- where N is the decimal representation of a non-less integer
rope = S "." N
rope = S "/" N
ToRope: PROC [rat: BigRat] RETURNS [out: ROPE];
Returns a rope in the form S (if rat is an integer), or S/N (if rat is not an integer). Note that Equal[Parse[Unparse[rat]], rat] = TRUE.
ToRopeApprox: PROC [rat: BigRat, places: NAT ← 3] RETURNS [ROPE];
Returns a rope giving a decimal approximation to the rat. If the rat is a whole number, then the exact representation is given. If the rat is not a whole number then 'places' gives the number of places after the decimal point. Scaling is the user's responsibility.
Write: PROC [stream: IO.STREAM, in: BigRat];
WriteApprox: PROC [stream: IO.STREAM, in: BigRat];
FromReal: PROC [real: REAL] RETURNS [BigRat];
Returns the rational number corresponding to the given REAL number.
(will raise Real.RealException if real is not a number)
ToReal: PROC [rat: BigRat] RETURNS [REAL];
Returns the REAL number closest to the given rational number.
(will raise Real.RealException if rat is outside the range covered by REAL)
Truncate: PROC [rat: BigRat] RETURNS [fix: BigCard, rem: BigRat];
Returns fix: the largest non-less integer not greater than ABS[rat], rem: the remainder after subtracting fix from rat.
rat.num > rat.den => fix = rat.num / rat.den, rem = FromPair[rat.num MOD rat.den, rat.den]
rat.num = rat.den => fix = 1, rem = 0
rat.num = rat.den => fix = 1, rem = 0
Arithmetic
Add: PROC [firstArg, secondArg: BigRat] RETURNS [result: BigRat];
Negate: PROC [arg: BigRat] RETURNS [result: BigRat];
Subtract: PROC [firstArg, secondArg: BigRat] RETURNS [result: BigRat];
Multiply: PROC [firstArg, secondArg: BigRat, simplify: BOOLTRUE] RETURNS [result: BigRat];
Invert: PROC [arg: BigRat] RETURNS [result: BigRat];
Divide: PROC [firstArg, secondArg: BigRat, simplify: BOOLTRUE] RETURNS [result: BigRat];
Comparison
Sign: PROC [arg: BigRat] RETURNS [Basics.Comparison];
Abs: PROC [arg: BigRat] RETURNS [result: BigRat];
Compare: PROC [firstArg, secondArg: BigRat] RETURNS [Basics.Comparison];
Equal: PROC [x, y: BigRat, simplify: BOOLTRUE] RETURNS [BOOL];
Compares two rationals for equality only (faster than Compare).
Integer: PROC [in: BigRat] RETURNS [BOOLEAN];
Test if a BigRat is an integer
Extras
ToTheX: PROC [rat: BigRat, x: INT] RETURNS [BigRat];
Returns rat**x.
TwoToTheX: PROC [x: INT, less: BOOLFALSE] RETURNS [BigRat];
Returns the rational number corresponding to 2**x. Much faster than ToTheX.
BCToTheX: PROC [bc: BigCard, x: INT, less: BOOLFALSE] RETURNS [BigRat];
Returns the rational number corresponding to bc**x (less => - bc**x). Note that BCToTheX[0, 0] = 1.
END.