home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / OB3.2D2.DMS / in.adf / Module / BigQuotients.mod < prev    next >
Encoding:
Text File  |  1994-08-05  |  3.4 KB  |  164 lines

  1. (*-------------------------------------------------------------------------*)
  2. (*                                                                         *)
  3. (*  Amiga Oberon Interface Module: BigQuotients       Date: 02-Nov-92      *)
  4. (*                                                                         *)
  5. (*   © 1992 by Fridtjof Siebert                                            *)
  6. (*                                                                         *)
  7. (*-------------------------------------------------------------------------*)
  8.  
  9. MODULE BigQuotients;
  10.  
  11. IMPORT BT * := BasicTypes, BI * := BigIntegers, Strings;
  12.  
  13. TYPE
  14.   BigQuotient * = POINTER TO BigQuotientDesc;
  15.   BigQuotientDesc * = RECORD (BT.FIELDDesc)
  16.     p-,
  17.     q-: BI.BigInteger;
  18.   END;
  19.  
  20.  
  21. VAR
  22.   zero: BI.BigInteger;
  23.  
  24.  
  25. PROCEDURE Create * (p,q: LONGINT): BigQuotient;
  26. (*
  27.  * Create new BigQuotient. Set its value to p/q.
  28.  *
  29.  * require
  30.  *   -MAX(LONGINT) <= p <= MAX(LONGINT);
  31.  *   -MAX(LONGINT) <= q <= MAX(LONGINT);
  32.  *   q#0;
  33.  *)
  34.  
  35. VAR
  36.   bi: BigQuotient;
  37.  
  38. BEGIN
  39.   NEW(bi);
  40.  
  41.   bi.p := BI.Create(p);
  42.   bi.q := BI.Create(q);
  43.  
  44.   RETURN bi;
  45. END Create;
  46.  
  47.  
  48. PROCEDURE (a: BigQuotient) Compare * (b: BT.COMPAREABLE): LONGINT;
  49. VAR
  50.   ap,bp: BT.COMPAREABLE;
  51. BEGIN
  52.   WITH b: BigQuotient DO
  53.     ap := a.p.Mul(b.q);
  54.     bp := b.p.Mul(a.q);
  55.     IF a.q.negative = b.q.negative THEN
  56.       RETURN   ap.Compare(bp);
  57.     ELSE
  58.       RETURN - ap.Compare(bp);
  59.     END;
  60.   END;
  61. END Compare;
  62.  
  63.  
  64. PROCEDURE (m: BigQuotient) Add * (n: BT.GROUP): BT.GROUP;
  65. VAR
  66.   result: BigQuotient;
  67.   g: BT.GROUP;
  68. BEGIN
  69.   NEW(result);
  70.   WITH n: BigQuotient DO
  71.     g := m.q.Mul(n.q); result.q := g(BI.BigInteger);
  72.     g := m.p.Mul(n.q); result.p := g(BI.BigInteger);
  73.     g := result.p.Add(n.p.Mul(m.q));
  74.     result.p := g(BI.BigInteger);
  75.   END;
  76.   RETURN result;
  77. END Add;
  78.  
  79.  
  80. PROCEDURE (m: BigQuotient) Neg * (        ): BT.GROUP;
  81. VAR
  82.   result: BigQuotient;
  83.   g: BT.GROUP;
  84. BEGIN
  85.   NEW(result);
  86.   g := m.p.Neg();     result.p := g(BI.BigInteger);
  87.   g := zero.Add(m.q); result.q := g(BI.BigInteger);
  88.   RETURN result;
  89. END Neg;
  90.  
  91.  
  92. PROCEDURE (m: BigQuotient) Norm * (): LONGREAL;
  93. BEGIN
  94.   RETURN m.p.Norm() / m.q.Norm();
  95. END Norm;
  96.  
  97.  
  98. PROCEDURE (m: BigQuotient) Mul * (n: BT.RING): BT.RING;
  99. VAR
  100.   result: BigQuotient;
  101.   r: BT.RING;
  102. BEGIN
  103.   NEW(result);
  104.   WITH n: BigQuotient DO
  105.     r := m.p.Mul(n.p); result.p := r(BI.BigInteger);
  106.     r := m.q.Mul(n.q); result.q := r(BI.BigInteger);
  107.   END;
  108.   RETURN result;
  109. END Mul;
  110.  
  111.  
  112. PROCEDURE (m: BigQuotient) Inv * (): BT.FIELD;
  113. VAR
  114.   result: BigQuotient;
  115.   g: BT.GROUP;
  116. BEGIN
  117.   NEW(result);
  118.   g := m.q.Neg(); result.p := g(BI.BigInteger);
  119.   g := m.p.Neg(); result.q := g(BI.BigInteger);
  120.   RETURN result;
  121. END Inv;
  122.  
  123.  
  124. PROCEDURE (m: BigQuotient) InvAllowed * (): BOOLEAN;
  125. BEGIN
  126.   RETURN m.p.Equal(zero);
  127. END InvAllowed;
  128.  
  129.  
  130. PROCEDURE (n: BigQuotient) ConvertToString * (): BT.DynString;
  131. (*
  132.  * Allocates space for to^ and converts n to an ASCII-String
  133.  *)
  134. VAR
  135.   ps,qs: BT.DynString;
  136.   p,q: BI.BigInteger;
  137.   g: BT.GROUP;
  138.   neg: BOOLEAN;
  139.   to: BT.DynString;
  140. BEGIN
  141.   p := n.p;
  142.   q := n.q;
  143.   neg := p.negative # q.negative;
  144.   IF p.negative THEN g := p.Neg(); p := g(BI.BigInteger) END;
  145.   IF q.negative THEN g := q.Neg(); q := g(BI.BigInteger) END;
  146.   ps := p.ConvertToString();
  147.   qs := q.ConvertToString();
  148.   NEW(to,LEN(ps^)+LEN(qs^)+2);
  149.   IF neg THEN to^ := "-"
  150.          ELSE to^ := " " END;
  151.   Strings.Append(to^,ps^);
  152.   Strings.AppendChar(to^,"/");
  153.   Strings.Append(to^,qs^);
  154.   RETURN to;
  155. END ConvertToString;
  156.  
  157.  
  158. BEGIN
  159.   zero := BI.Create(0);
  160. END BigQuotients.
  161.  
  162.  
  163.  
  164.