5 * A really shoddy implementation of Rational numbers
16 template <class T> T Tabs(const T & a)
21 template <> Arbint Tabs(const Arbint & a);
24 /* Recursive version of GCD
26 T gcd(const T & a, const T & b)
28 Debug("Called on %li/%li", int64_t(a), int64_t(b));
29 if (a == T(1) || a == T(0)) return T(1);
30 if (b == T(0)) return a;
38 if (a > b) return gcd(a-b,b);
43 /** Greatest Common Divisor of p and q **/
45 T gcd(const T & p, const T & q)
59 while ((g = big % small) > T(0))
61 //Debug("big = %li, small = %li", int64_t(big), int64_t(small));
64 //Debug("Loop %u", ++count);
70 template <class T = int64_t>
73 /** Construct from a double.**/
74 Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
79 Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
84 Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
107 T g = gcd(Tabs(P), Tabs(Q));
113 bool operator==(const Rational & r) const
115 if (P == r.P && Q == r.Q) return true;
116 return ToDouble() == r.ToDouble();
120 bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
121 bool operator>(const Rational & r) const {return !(*this < r);}
122 bool operator<=(const Rational & r) const {return *this == r || *this < r;}
123 bool operator>=(const Rational & r) const {return *this == r || *this > r;}
124 bool operator!=(const Rational & r) const {return !(*this == r);}
126 Rational operator+(const Rational & r) const
128 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
129 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"+"))
131 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
135 Rational operator-(const Rational & r) const
137 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
138 result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
141 Rational operator*(const Rational & r) const
143 Rational result(P * r.P, Q * r.Q);
144 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
146 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
150 Rational operator/(const Rational & r) const
152 Rational result(P * r.Q, Q*r.P);
153 //if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
155 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
160 /** To cheat, use these **/
161 //Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());}
162 //Rational operator-(const Rational & r) const {return Rational(ToDouble()-r.ToDouble());}
163 //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());}
164 //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
166 Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; Simplify(); return *this;}
167 Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
168 Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
169 Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
170 Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
172 double ToDouble() const {return (double)(P) / (double)(Q);}
173 bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
175 double result = fabs(ToDouble() - d);
176 if (d != 0e0) result /= d;
177 if (result > threshold)
179 Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
185 std::string Str() const
188 s << int64_t(P) << "/" << int64_t(Q);
196 inline Rational<int64_t> pow(const Rational<int64_t> & a, const Rational<int64_t> & b)
198 //TODO:Implement properly
199 int64_t P = std::pow((double)a.P, b.ToDouble());
200 int64_t Q = std::pow((double)a.Q, b.ToDouble());
201 return Rational<int64_t>(P, Q);