rational.h
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 9k
Development Platform:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  rational.h
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. #ifndef LEDA_rational_H
  12. #define LEDA_rational_H
  13. /*{Manpage {rational} {} {Rational Numbers}}*/
  14. #include <LEDA/integer.h>
  15. class rational
  16. {
  17. /*{Mdefinition An instance $q$ of type name is a rational number
  18. where the numerator and the denominator are both of type
  19. $integer$.}*/
  20. protected:
  21.   integer num; // numerator
  22.   integer den; // denominator, always nonzero and positive
  23. public:
  24. /*{Mcreation q}*/
  25.   rational();
  26. /*{Mcreate creates an instance var of type name.}*/
  27.   rational(double x);
  28. /*{Mcreate creates an instance var of type name and initializes it
  29. with the value of $x$.}*/
  30.   rational(int n);
  31. /*{Mcreate creates an instance var of type name and initializes it
  32. with the value of $n$.}*/
  33.   rational(int m, int n);
  34. /*{Mcreate creates an instance var of type name and initializes its
  35. numerator with $m$ and its denominator with $n$.}*/
  36.   rational(const integer& a);
  37. /*{Mcreate creates an instance var of type name and initializes it
  38. with the value of $a$.}*/
  39.   rational(const integer& a, const integer& b);
  40. /*{Mcreate creates an instance var of type name and initializes its
  41. numerator with $a$ and its denominator with $b$.}*/
  42.   rational(const rational&);
  43.   rational& operator= (const rational&);
  44.  ~rational();
  45. /*{Moperations 2 5}*/
  46. /*{Mtext
  47. The arithmetic operations $+, -, *, /, +=,
  48. -=, *=, /=, -$(unary), $++, --$,  
  49. the comparison operations $<, <=, >, 
  50. >=, ==, !=$ and the stream operations are all available.}*/
  51. // friend functions, first arithmetic operators
  52.   friend inline rational operator+ (rational q, const rational& r);
  53. /*{Xfunc returns var $+ r$.}*/
  54.   friend inline rational operator- (rational q, const rational& r);
  55. /*{Xfunc returns var $- r$.}*/
  56.   friend inline rational operator* (rational q, const rational& r);
  57. /*{Xfunc returns var $* r$.}*/
  58.   friend inline rational operator/ (rational q, const rational& r);
  59.   
  60. /*{Xfunc returns var $/ r$.}*/
  61. // unary minus 
  62.   friend inline rational operator- (const rational&);
  63. /*{Xfunc returns --var.}*/
  64.   rational& operator+= (const rational& r);
  65. /*{Xbinop returns var $+ r$.}*/
  66.   rational& operator-= (const rational&);
  67.   
  68. /*{Xbinop returns var $- r$.}*/
  69.   rational& operator*= (const rational&);
  70. /*{Xbinop returns var $* r$.}*/
  71.   rational& operator/= (const rational&);
  72. /*{Xbinop returns var $/ r$.}*/
  73.   rational& operator++ ();
  74. /*{Xunop increments var by $1$.}*/
  75.   rational& operator-- ();
  76. /*{Xunop decrements var by $1$.}*/
  77. // comparison operators
  78.   static int cmp(const rational&, const rational&);
  79.   friend inline bool operator== (const rational& q, const rational& r);
  80. /*{Xfunc Equality test.}*/
  81.   friend inline bool operator== (const rational& q, int n);
  82. /*{Xfunc Equality test.}*/
  83.   friend inline bool operator== (int n, const rational& q);
  84. /*{Xfunc Equality test.}*/
  85.   friend inline bool operator!= (const rational& q, const rational& r);
  86. /*{Xfunc Inequality test.}*/
  87.   friend inline bool operator!= (const rational& q, int n);
  88. /*{Xfunc Inequality test.}*/
  89.   friend inline bool operator!= (int n, const rational& q);
  90. /*{Xfunc Inequality test.}*/
  91.   friend inline bool operator< (const rational& q, const rational& r);
  92. /*{Xfunc returns $q < r$.}*/
  93.   friend inline bool operator<= (const rational& q, const rational& r);
  94. /*{Xfunc returns $q <= r$.}*/
  95.   friend inline bool operator> (const rational& q, const rational& r);
  96. /*{Xfunc returns $q > r$.}*/
  97.   friend inline bool operator>= (const rational& q, const rational& r);
  98. /*{Xfunc returns $q >= r$.}*/
  99.   integer numerator() const;
  100. /*{Mop returns the numerator of $q$.}*/
  101.   integer denominator() const;
  102. /*{Mop returns the denominator of $q$.}*/
  103.   rational& simplify(const integer& a);
  104. /*{Mop simplifies $q$ by $a$.\
  105.         precond $a$ divides the numerator and the denominator of $q$.}*/
  106.   rational& normalize();
  107. /*{Mop normalizes var.}*/
  108.   void negate(); 
  109. /*{Mop negates var.}*/
  110.   void invert(); 
  111. /*{Mop inverts var.}*/
  112.   rational inverse();  
  113. /*{Mop returns the inverse of var.}*/
  114. /*{Mtext
  115. smallskip
  116. {bf Non-member functions}
  117. smallskip }*/
  118.   friend inline int sign(const rational& q);
  119. /*{Mfunc returns the sign of $q$.}*/
  120.   friend inline rational abs(const rational& q);
  121. /*{Mfunc returns the absolute value of $q$.}*/
  122.   friend inline rational sqr(const rational& q);
  123. /*{Mfunc returns the square of $q$.}*/
  124.   friend inline integer trunc(const rational& q);
  125. /*{Mfunc returns the $integer$ with the next smaller absolute value.}*/
  126.   friend rational pow(const rational& q, int n); 
  127. /*{Mfunc returns the $n$-th power of $q$.}*/
  128.   friend rational pow(const rational& q, integer a); 
  129. /*{Mfunc returns the $a$-th power of $q$.}*/
  130.   friend integer floor(const rational& q);
  131. /*{Mfunc returns the next smaller $integer$.}*/
  132.   friend integer ceil(const rational& q);
  133. /*{Mfunc returns the next bigger $integer$.}*/
  134.   friend integer round(const rational& q);
  135. /*{Mfunc rounds $q$ to the nearest $integer$.}*/
  136. // comparison functions
  137. /*
  138.   friend bool LRge0(const rational&);
  139.   friend bool LRgt0(const rational&);
  140.   friend bool LRle0(const rational&);
  141.   friend bool LRlt0(const rational&);
  142.   friend bool LReq0(const rational&);
  143.   friend bool LReq1(const rational&);
  144. */
  145. // conversion
  146. //  operator double () const; // rational to double
  147. // input/output
  148.   friend inline istream& operator>> (istream& in, rational& q);
  149. /*{Xfunc reads var from the istream $in$.}*/
  150.   friend inline ostream& operator<< (ostream& out, const rational& q);
  151. /*{Xfunc writes var to the ostream $out$.}*/
  152. };
  153. inline int compare(const rational& x, const rational& y)
  154. { return rational::cmp(x,y); } 
  155.   inline rational::rational()
  156.     { num = 0; den = 1; }
  157.   inline rational::rational(int n)
  158.     { num = integer(n); den = 1; }
  159.   inline rational::rational(const integer& i)
  160.     { num = i; den = 1; }
  161.   inline rational::rational(const rational& r)
  162.     { num = r.num; den = r.den; }
  163.   inline rational::~rational() {}
  164.   inline rational& rational::operator++ ()
  165.     { num += den; return (*this).normalize(); }
  166.   inline rational& rational::operator-- ()
  167.     { num -= den; return (*this).normalize(); }
  168.   inline integer rational::numerator() const
  169.     { return num; }
  170.   inline integer rational::denominator() const
  171.     { return den; }
  172.   inline void rational::negate()
  173.     { num = - num; }
  174.   inline rational operator+ (rational x, const rational& y)
  175.     { return x += y; }
  176.   inline rational operator- (rational x, const rational& y)
  177.     { return x -= y; }
  178.   inline rational operator* (rational x, const rational& y)
  179.     { return x *= y; }
  180.   inline rational operator/ (rational x, const rational& y)
  181.     { return x /= y; }
  182.   inline rational operator- (const rational& x)
  183.     { return rational(-x.num,x.den); }
  184.   inline int sign(const rational& r)
  185.     { return sign(r.num); }
  186.   inline rational abs(const rational& r)
  187.     { if (sign(r.num) > -1) { return r; } else { return -r; } }
  188.   inline rational sqr(const rational& r)
  189.     { return rational(r.num*r.num, r.den*r.den); }
  190.   inline integer trunc(const rational& r)
  191.     { return (r.num / r.den); }
  192.   inline ostream& operator<< (ostream& s, const rational& r)
  193.     {  s << r.num << "/" << r.den; return s; }
  194.   inline bool operator== (const rational& x, const rational& y)
  195.     { return ((x.num == y.num) && (x.den == y.den)); }
  196.   inline bool operator== (const rational& x, int y)
  197.     { return ((x.den==1) && (x.num == integer(y))); }
  198.   inline bool operator== (int x, const rational& y)
  199.     { return ((y.den==1) && (y.num == integer(x))); }
  200.   inline bool operator!= (const rational& x, const rational& y)
  201.     { return ((x.num != y.num) || (x.den != y.den)); }
  202.   inline bool operator!= (const rational& x, int y)
  203.     { return ((x.den != 1) || (x.num != integer(y))); }
  204.   inline bool operator!= (int x, const rational& y)
  205.     { return ((y.den != 1) || (y.num != integer(x))); }
  206.   inline bool operator< (const rational& x, const rational& y)
  207.     { return rational::cmp(x,y) < 0; }
  208.   inline bool operator<= (const rational& x, const rational& y)
  209.     { return rational::cmp(x,y) <= 0; }
  210.   inline bool operator> (const rational& x, const rational& y)
  211.     { return rational::cmp(x,y) > 0; }
  212.   inline bool operator>= (const rational& x, const rational& y)
  213.     { return rational::cmp(x,y) >= 0; }
  214. /*{Mimplementation A $rational$ is implemented by two $integer$
  215. numbers which represent the numerator and the denominator. The sign
  216. is represented by the sign of the numerator.}*/
  217. #endif