home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / rational.cc < prev    next >
C/C++ Source or Header  |  1993-07-23  |  6KB  |  379 lines

  1. /* 
  2. Copyright (C) 1988 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU CC General Public
  12. License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. GNU CC, but only under the conditions described in the
  16. GNU CC General Public License.   A copy of this license is
  17. supposed to have been given to you along with GNU CC so you
  18. can know your rights and responsibilities.  It should be in a
  19. file named COPYING.  Among other things, the copyright notice
  20. and this notice must be preserved on all copies.  
  21. */
  22.  
  23. #include <Rational.h>
  24. #include <std.h>
  25. #include <math.h>
  26. #include "libconfig.h"
  27.  
  28.  
  29.  
  30. void Rational::error(char* msg)
  31. {
  32.   (*lib_error_handler)("Rational", msg);
  33. }
  34.  
  35. static Integer _Int_One(1);
  36.  
  37. void Rational::normalize()
  38. {
  39.   int s = sign(den);
  40.   if (s == 0)
  41.     error("Zero denominator.");
  42.   else if (s < 0)
  43.   {
  44.     den.negate();
  45.     num.negate();
  46.   }
  47.  
  48.   Integer g = gcd(num, den);
  49.   if (ucompare(g, _Int_One) != 0)
  50.   {
  51.     num /= g;
  52.     den /= g;
  53.   }
  54. }
  55.  
  56. RatTmp Rational::operator + (Rational& y)
  57. {
  58.   return RatTmp(num * y.den + den * y.num, den * y.den);
  59. }
  60.  
  61. RatTmp Rational::operator - (Rational& y)
  62. {
  63.   return RatTmp(num * y.den - den * y.num, den * y.den);
  64. }
  65.  
  66. RatTmp Rational::operator * (Rational& y)
  67. {
  68.   return RatTmp(num * y.num, den * y.den);
  69. }
  70.  
  71. RatTmp Rational::operator / (Rational& y)
  72. {
  73.   return RatTmp(num * y.den, den * y.num);
  74. }
  75.  
  76. RatTmp Rational::operator - ()
  77. {
  78.   Integer d = den;
  79.   return RatTmp(-num, d);
  80. }
  81.  
  82. RatTmp RatTmp::operator - ()
  83. {
  84.   num.negate();
  85.   return *this;
  86. }
  87.  
  88. RatTmp abs(Rational& x)
  89. {
  90.   Integer d = x.den;
  91.   return RatTmp(abs(x.num), d);
  92. }
  93.  
  94. RatTmp abs(RatTmp& x)
  95. {
  96.   x.num.abs();
  97.   return x;
  98. }
  99.  
  100. RatTmp sqr(Rational& x)
  101. {
  102.   return RatTmp(x.num * x.num, x.den * x.den);
  103. }
  104.  
  105. RatTmp sqr(RatTmp& x)
  106. {
  107.   x.num *= x.num;
  108.   x.den *= x.den;
  109.   return x;
  110. }
  111.  
  112. void Rational::operator +=(Rational& y)
  113. {
  114.   num = num * y.den + den * y.num;
  115.   den *= y.den;
  116.   normalize();
  117. }
  118.  
  119. void Rational::operator -=(Rational& y)
  120. {
  121.   num = num * y.den - den * y.num;
  122.   den *= y.den;
  123.   normalize();
  124. }
  125.  
  126. void Rational::operator *=(Rational& y)
  127. {
  128.   num *= y.num;
  129.   den *= y.den;
  130.   normalize();
  131. }
  132.  
  133. void Rational::operator /=(Rational& y)
  134. {
  135.   if (&y == this)
  136.   {
  137.     Integer n = num * y.den;
  138.     den *= y.num;
  139.     num = n;
  140.   }
  141.   else
  142.   {
  143.     num *= y.den;
  144.     den *= y.num;
  145.   }
  146.   normalize();
  147. }
  148.  
  149. RatTmp RatTmp::operator + (Rational& y)
  150. {
  151.   num = num * y.den + den * y.num;
  152.   den *= y.den;
  153.   normalize();
  154.   return *this;
  155. }
  156.  
  157. RatTmp RatTmp::operator - (Rational& y)
  158. {
  159.   num = num * y.den - den * y.num;
  160.   den *= y.den;
  161.   normalize();
  162.   return *this;
  163. }
  164.  
  165. RatTmp RatTmp::operator * (Rational& y)
  166. {
  167.   num *= y.num;
  168.   den *= y.den;
  169.   normalize();
  170.   return *this;
  171. }
  172.  
  173. RatTmp RatTmp::operator / (Rational& y)
  174. {
  175.   if (&y == this)
  176.   {
  177.     Integer n = num * y.den;
  178.     den *= y.num;
  179.     num = n;
  180.   }
  181.   else
  182.   {
  183.     num *= y.den;
  184.     den *= y.num;
  185.   }
  186.   normalize();
  187.   return *this;
  188. }
  189.  
  190. void Rational::invert()
  191. {
  192.   Integer tmp = num;  
  193.   num = den;  
  194.   den = tmp;  
  195.   int s = sign(den);
  196.   if (s == 0)
  197.     error("Zero denominator.");
  198.   else if (s < 0)
  199.   {
  200.     den.negate();
  201.     num.negate();
  202.   }
  203. }
  204.  
  205. int compare(Rational& x, Rational& y)
  206. {
  207.   int xsgn = sign(x.num);
  208.   int ysgn = sign(y.num);
  209.   int d = xsgn - ysgn;
  210.   if (d == 0 && xsgn != 0) d = compare(x.num * y.den, x.den * y.num);
  211.   return d;
  212. }
  213.  
  214. Rational::Rational(double x)
  215. {
  216.   num = 0;
  217.   den = 1;
  218.   if (x != 0.0)
  219.   {
  220.     int neg = x < 0;
  221.     if (neg)
  222.       x = -x;
  223.  
  224.     const long shift = 15;         // a safe shift per step
  225.     const double width = 32768.0;  // = 2^shift
  226.     const int maxiter = 20;        // ought not be necessary, but just in case,
  227.                                    // max 300 bits of precision
  228.     int expt;
  229.     double mantissa = frexp(x, &expt);
  230.     long exponent = expt;
  231.     double intpart;
  232.     int k = 0;
  233.     while (mantissa != 0.0 && k++ < maxiter)
  234.     {
  235.       mantissa *= width;
  236.       mantissa = modf(mantissa, &intpart);
  237.       num <<= shift;
  238.       num += (long)intpart;
  239.       exponent -= shift;
  240.     }
  241.     if (exponent > 0)
  242.       num <<= exponent;
  243.     else if (exponent < 0)
  244.       den <<= -exponent;
  245.     if (neg)
  246.       num.negate();
  247.   }
  248.   normalize();
  249. }
  250.  
  251. IntTmp floor(Rational& x)
  252. {
  253.   Integer q, r;
  254.   divide(x.num, x.den, q, r);
  255.   if (sign(x.num) < 0 && sign(r) != 0) q--;
  256.   return q;
  257. }
  258.  
  259. IntTmp ceil(Rational& x)
  260. {
  261.   Integer q, r;
  262.   divide(x.num, x.den, q, r);
  263.   if (sign(x.num) >= 0 && sign(r) != 0) q++;
  264.   return q;
  265. }
  266.  
  267. IntTmp round(Rational& x)
  268. {
  269.   Integer q, r;
  270.   divide(x.num, x.den, q, r);
  271.   r <<= 1;
  272.   if (ucompare(r, x.den) >= 0)
  273.   {
  274.     if (sign(x.num) >= 0)
  275.       q++;
  276.     else
  277.       q--;
  278.   }
  279.   return q;
  280. }
  281.  
  282. IntTmp trunc(Rational& x)
  283. {
  284.   return x.num / x.den ;
  285. }
  286.  
  287. IntTmp Rational::numerator()
  288. {
  289.   Integer n = num;
  290.   return n;
  291. }
  292.  
  293. IntTmp Rational::denominator()
  294. {
  295.   Integer d = den;
  296.   return d;
  297. }
  298.  
  299. RatTmp pow(Rational& x, Integer& y)
  300. {
  301.   long yy = long(y);
  302.   return pow(x, yy);
  303. }               
  304.  
  305. double Rational::operator double()
  306. {
  307.   return ratio(num, den);
  308. }
  309.  
  310.  
  311. // power: no need to normalize since num & den already relatively prime
  312.  
  313. RatTmp pow(Rational& x, long y)
  314. {
  315.   Rational r;
  316.   if (y >= 0)
  317.   {
  318.     r.num = pow(x.num, y);
  319.     r.den = pow(x.den, y);
  320.   }
  321.   else
  322.   {
  323.     y = -y;
  324.     r.den = pow(x.num, y);
  325.     r.num = pow(x.den, y);
  326.     if (sign(r.den) < 0)
  327.     {
  328.       r.num.negate();
  329.       r.den.negate();
  330.     }
  331.   }
  332.   return r;
  333. }
  334.  
  335.  
  336. ostream& operator << (ostream& s, Rational& y)
  337. {
  338.   if (y.den == 1)
  339.     s << Itoa(y.num);
  340.   else
  341.   {
  342.     s << Itoa(y.num);
  343.     s << "/";
  344.     s << Itoa(y.den);
  345.   }
  346.   return s;
  347. }
  348.  
  349. istream& operator >> (istream& s, Rational& y)
  350. {
  351.   s >> y.num;
  352.   if (s)
  353.   {
  354.     char ch = 0;
  355.     s.get(ch);
  356.     if (ch == '/')
  357.     {
  358.       s >> y.den;
  359.       y.normalize();
  360.     }
  361.     else
  362.     {
  363.       s.unget(ch);
  364.       y.den = 1;
  365.     }
  366.   }
  367.   return s;
  368. }
  369.  
  370. int Rational::OK()
  371. {
  372.   int v = num.OK() && den.OK(); // have valid num and denom
  373.   v &= sign(den) > 0;           // denominator positive;
  374.   v &=  ucompare(gcd(num, den), _Int_One) == 0; // relatively prime
  375.   if (!v) error("invariant failure");
  376.   return v;
  377. }
  378.  
  379.