00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _cvc3__rational_h_
00030 #define _cvc3__rational_h_
00031
00032
00033
00034
00035 #include <vector>
00036 #include "debug.h"
00037
00038
00039 namespace CVC3 {
00040
00041 class CVC_DLL Rational {
00042 private:
00043 class Impl;
00044 Impl *d_n;
00045
00046 #ifdef _DEBUG_RATIONAL_
00047
00048
00049 int& getCreated() {
00050 static int num_created = 0;
00051 return(num_created);
00052 }
00053
00054 int& getDeleted() {
00055 static int num_deleted = 0;
00056 return(num_deleted);
00057 }
00058
00059 void printStats();
00060 #endif
00061
00062 Rational(const Impl& t);
00063
00064 public:
00065
00066 Rational();
00067
00068 Rational(const Rational &n);
00069 Rational(int n, int d = 1);
00070 Rational(const char* n, int base = 10);
00071 Rational(const std::string& n, int base = 10);
00072 Rational(const char* n, const char* d, int base = 10);
00073 Rational(const std::string& n, const std::string& d, int base = 10);
00074
00075 ~Rational();
00076
00077
00078 Rational& operator=(const Rational& n);
00079
00080 std::string toString(int base = 10) const;
00081
00082
00083 size_t hash() const;
00084
00085 friend CVC_DLL bool operator==(const Rational &n1, const Rational &n2);
00086 friend CVC_DLL bool operator<(const Rational &n1, const Rational &n2);
00087 friend CVC_DLL bool operator<=(const Rational &n1, const Rational &n2);
00088 friend CVC_DLL bool operator>(const Rational &n1, const Rational &n2);
00089 friend CVC_DLL bool operator>=(const Rational &n1, const Rational &n2);
00090 friend CVC_DLL bool operator!=(const Rational &n1, const Rational &n2);
00091 friend CVC_DLL Rational operator+(const Rational &n1, const Rational &n2);
00092 friend CVC_DLL Rational operator-(const Rational &n1, const Rational &n2);
00093 friend CVC_DLL Rational operator*(const Rational &n1, const Rational &n2);
00094 friend CVC_DLL Rational operator/(const Rational &n1, const Rational &n2);
00095
00096 friend CVC_DLL Rational operator%(const Rational &n1, const Rational &n2);
00097
00098
00099 Rational operator-() const;
00100 Rational &operator+=(const Rational &n2);
00101 Rational &operator-=(const Rational &n2);
00102 Rational &operator*=(const Rational &n2);
00103 Rational &operator/=(const Rational &n2);
00104
00105 const Rational& operator++() { *this = (*this)+1; return *this; }
00106
00107 Rational operator++(int) { Rational x(*this); *this = x+1; return x; }
00108
00109 const Rational& operator--() { *this = (*this)-1; return *this; }
00110
00111 Rational operator--(int) { Rational x(*this); *this = x-1; return x; }
00112
00113
00114 Rational getNumerator() const;
00115 Rational getDenominator() const;
00116
00117
00118 bool isInteger() const;
00119
00120 int getInt() const;
00121
00122 bool isUnsigned() const { return (isInteger() && (*this) >= 0); }
00123
00124 unsigned int getUnsigned() const;
00125
00126 friend std::ostream &operator<<(std::ostream &os, const Rational &n);
00127
00128
00129
00130
00131 friend CVC_DLL Rational gcd(const Rational &x, const Rational &y);
00132 friend CVC_DLL Rational gcd(const std::vector<Rational> &v);
00133 friend CVC_DLL Rational lcm(const Rational &x, const Rational &y);
00134 friend CVC_DLL Rational lcm(const std::vector<Rational> &v);
00135
00136 friend CVC_DLL Rational abs(const Rational &x);
00137
00138
00139 friend CVC_DLL Rational floor(const Rational &x);
00140
00141 friend CVC_DLL Rational ceil(const Rational &x);
00142
00143 friend CVC_DLL Rational mod(const Rational &x, const Rational &y);
00144
00145 friend CVC_DLL Rational intRoot(const Rational& base, unsigned long int n);
00146
00147
00148 void print() const;
00149
00150 };
00151
00152
00153 inline Rational pow(Rational pow, const Rational& base) {
00154 DebugAssert(pow.isInteger(), "pow("+pow.toString()
00155 +", "+base.toString()+")");
00156 FatalAssert(base != 0 || pow >= 0, "Attempt to divide by zero");
00157 bool neg(pow < 0);
00158 if(neg) pow = -pow;
00159 Rational res(1);
00160 for(; pow > 0; --pow) res *= base;
00161 if(neg) res = 1/res;
00162 return res;
00163 }
00164
00165
00166 inline Rational ratRoot(const Rational& base, unsigned long int n)
00167 {
00168 DebugAssert(base != 0, "Expected nonzero base");
00169 Rational num = base.getNumerator();
00170 num = intRoot(num, n);
00171 if (num != 0) {
00172 Rational den = base.getDenominator();
00173 den = intRoot(den, n);
00174 if (den != 0) {
00175 return num / den;
00176 }
00177 }
00178 return 0;
00179 }
00180
00181
00182
00183 inline Rational newRational(int n, int d = 1) { return Rational(n, d); }
00184 inline Rational newRational(const char* n, int base = 10)
00185 { return Rational(n, base); }
00186 inline Rational newRational(const std::string& n, int base = 10)
00187 { return Rational(n, base); }
00188 inline Rational newRational(const char* n, const char* d, int base = 10)
00189 { return Rational(n, d, base); }
00190 inline Rational newRational(const std::string& n, const std::string& d,
00191 int base = 10)
00192 { return Rational(n, d, base); }
00193
00194
00195 void printRational(const Rational &x);
00196
00197
00198 typedef unsigned long Unsigned;
00199
00200 }
00201
00202 #endif