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

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /* 
  4. Copyright (C) 1988 Free Software Foundation
  5.     written by Doug Lea (dl@rocky.oswego.edu)
  6.  
  7. This file is part of GNU CC.
  8.  
  9. GNU CC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the GNU CC General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. GNU CC, but only under the conditions described in the
  18. GNU CC General Public License.   A copy of this license is
  19. supposed to have been given to you along with GNU CC so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies.  
  23. */
  24.  
  25. #ifndef _Rational_h
  26. #pragma once
  27. #define _Rational_h 1
  28.  
  29. #include <Integer.h>
  30. #include <math.h>
  31.  
  32. class RatTmp;
  33.  
  34. class Rational
  35. {
  36.   friend class     RatTmp;
  37. protected:
  38.   Integer          num;
  39.   Integer          den;
  40.  
  41.   void             normalize();
  42.  
  43. public:
  44.                    Rational();
  45.                    Rational(double);
  46.                    Rational(long n, long d = 1);
  47.                    Rational(Integer& n);
  48.                    Rational(Integer& n, Integer& d);
  49.                    Rational(Rational&);
  50.  
  51.                    ~Rational();
  52.  
  53.   void             operator =  (Rational& y);
  54.  
  55.   friend int       operator == (Rational& x, Rational& y);
  56.   friend int       operator != (Rational& x, Rational& y);
  57.   friend int       operator <  (Rational& x, Rational& y);
  58.   friend int       operator <= (Rational& x, Rational& y);
  59.   friend int       operator >  (Rational& x, Rational& y);
  60.   friend int       operator >= (Rational& x, Rational& y);
  61.  
  62.   RatTmp           operator +  (Rational& x);
  63.   void             operator += (Rational& y);
  64.   RatTmp           operator -  (Rational& y);
  65.   void             operator -= (Rational& y);
  66.   RatTmp           operator *  (Rational& y);
  67.   void             operator *= (Rational& y);
  68.   RatTmp           operator /  (Rational& x);
  69.   void             operator /= (Rational& y);
  70.  
  71.   friend Rational& operator <? (Rational& x, Rational& y); // min
  72.   friend Rational& operator >? (Rational& x, Rational& y); // max
  73.  
  74.   RatTmp           operator -  ();
  75.  
  76.  
  77. // builtin Rational functions
  78.  
  79.  
  80.   void             negate();                      // x = -x
  81.   void             invert();                      // x = 1/x
  82.  
  83.   friend int       sign(Rational& x);             // -1, 0, or +1
  84.   friend RatTmp    abs(Rational& x);              // absolute value
  85.   friend RatTmp    sqr(Rational& x);              // square
  86.   friend RatTmp    pow(Rational& x, long y);
  87.   friend RatTmp    pow(Rational& x, Integer& y);
  88.   IntTmp           numerator();
  89.   IntTmp           denominator();
  90.  
  91. // coercion & conversion
  92.  
  93.   double           operator double();
  94.   friend IntTmp    floor(Rational& x);
  95.   friend IntTmp    ceil(Rational& x);
  96.   friend IntTmp    trunc(Rational& x);
  97.   friend IntTmp    round(Rational& x);
  98.  
  99.   friend istream&  operator >> (istream& s, Rational& y);
  100.   friend ostream&  operator << (ostream& s, Rational& y);
  101.  
  102. // miscellany
  103.  
  104.   friend int       compare(Rational& x, Rational& y);
  105.   void             error(char* msg);
  106.   int              OK();
  107.  
  108. };
  109.  
  110. class RatTmp : public Rational
  111. {
  112. public:
  113.                    RatTmp(Integer& n, Integer& d);
  114.                    RatTmp(Rational&);
  115.                    RatTmp(RatTmp&);
  116.  
  117.                    ~RatTmp();
  118.  
  119.   RatTmp           operator +  (Rational& x);
  120.   RatTmp           operator -  (Rational& y);
  121.   RatTmp           operator *  (Rational& y);
  122.   RatTmp           operator /  (Rational& x);
  123.  
  124.   RatTmp           operator -  ();
  125.  
  126.   friend RatTmp    abs(RatTmp& x);
  127.   friend RatTmp    sqr(RatTmp& x);
  128. };
  129.  
  130.  
  131. //#ifdef __OPTIMIZE__
  132.  
  133. inline Rational::Rational()  {}
  134. inline Rational::~Rational() {}
  135. inline RatTmp::~RatTmp() {}
  136.  
  137. inline Rational::Rational(Rational& y)
  138. {
  139.   num = y.num; den = y.den;
  140. }
  141.  
  142. inline RatTmp::RatTmp(Rational& y)
  143. {
  144.   num = IntTmp(y.num); den = IntTmp(y.den);
  145. }
  146.  
  147. inline RatTmp::RatTmp(RatTmp& y)
  148. {
  149.   num = IntTmp(y.num); den = IntTmp(y.den);
  150. }
  151.  
  152. inline Rational::Rational(Integer& n)
  153. {
  154.   num = n; den = 1;
  155. }
  156.  
  157. inline Rational::Rational(Integer& n, Integer& d)
  158. {
  159.   num = n; den = d; normalize();
  160. }
  161.  
  162. inline RatTmp::RatTmp(Integer& n, Integer& d)
  163. {
  164.   num = IntTmp(n); den = IntTmp(d); normalize();
  165. }
  166.  
  167. inline Rational::Rational(long n, long d = 1)
  168. {
  169.   num = n; den = d; normalize();
  170. }
  171.  
  172. inline  void Rational::operator =  (Rational& y)
  173. {
  174.   num = y.num;  den = y.den;
  175. }
  176.  
  177. inline int operator == (Rational& x, Rational& y)
  178. {
  179.   return compare(x.num, y.num) == 0 && compare(x.den, y.den) == 0;
  180. }
  181.  
  182. inline int operator != (Rational& x, Rational& y)
  183. {
  184.   return compare(x.num, y.num) != 0 || compare(x.den, y.den) != 0;
  185. }
  186.  
  187. inline int operator <  (Rational& x, Rational& y)
  188. {
  189.   return compare(x, y) <  0; 
  190. }
  191.  
  192. inline int operator <= (Rational& x, Rational& y)
  193. {
  194.   return compare(x, y) <= 0; 
  195. }
  196.  
  197. inline int operator >  (Rational& x, Rational& y)
  198. {
  199.   return compare(x, y) >  0; 
  200. }
  201.  
  202. inline int operator >= (Rational& x, Rational& y)
  203. {
  204.   return compare(x, y) >= 0; 
  205. }
  206.  
  207. inline int sign(Rational& x)
  208. {
  209.   return sign(x.num);
  210. }
  211.  
  212. inline void Rational::negate()
  213. {
  214.   num.negate();
  215. }
  216.  
  217. inline Rational& operator <? (Rational& x, Rational& y)
  218. {
  219.   if (compare(x, y) <= 0) return x; else return y;
  220. }
  221.  
  222. inline Rational& operator >? (Rational& x, Rational& y)
  223. {
  224.   if (compare(x, y) >= 0) return x; else return y;
  225. }
  226.  
  227.  
  228. //#endif
  229.  
  230. #endif
  231.