5 * A really shoddy implementation of Rational numbers
15 /* Recursive version of GCD
17 T gcd(const T & a, const T & b)
19 Debug("Called on %li/%li", int64_t(a), int64_t(b));
20 if (a == T(1) || a == T(0)) return T(1);
21 if (b == T(0)) return a;
29 if (a > b) return gcd(a-b,b);
34 /** Greatest Common Divisor of p and q **/
36 T gcd(const T & p, const T & q)
50 while ((g = big % small) > T(0))
52 //Debug("big = %li, small = %li", int64_t(big), int64_t(small));
55 //Debug("Loop %u", ++count);
61 template <class T = int64_t>
64 /** Construct from a double.**/
65 Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
68 if (!CheckAccuracy(d, "Construct from double"))
70 //Fatal("Bwah bwah :(");
74 Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
79 Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
96 T g = gcd(T(llabs(P)),T(llabs(Q)));
102 bool operator==(const Rational & r) const
104 if (P == r.P && Q == r.Q) return true;
105 return ToDouble() == r.ToDouble();
109 bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
110 bool operator>(const Rational & r) const {return !(*this < r);}
111 bool operator<=(const Rational & r) const {return *this == r || *this < r;}
112 bool operator>=(const Rational & r) const {return *this == r || *this > r;}
113 bool operator!=(const Rational & r) const {return !(*this == r);}
115 Rational operator+(const Rational & r) const
117 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
118 if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"+"))
120 Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
124 Rational operator-(const Rational & r) const
126 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
127 result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
130 Rational operator*(const Rational & r) const
132 Rational result(P * r.P, Q * r.Q);
133 if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
135 Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
139 Rational operator/(const Rational & r) const
141 Rational result(P * r.Q, Q*r.P);
142 if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
144 Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
149 /** To cheat, use these **/
150 //Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());}
151 //Rational operator-(const Rational & r) const {return Rational(ToDouble()-r.ToDouble());}
152 //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());}
153 //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
155 Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; return *this;}
156 Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
157 Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
158 Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
159 Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
161 double ToDouble() const {return (double)(P) / (double)(Q);}
162 bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
164 double result = fabs(ToDouble() - d);
165 if (d != 0e0) result /= d;
166 if (result > threshold)
168 Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
174 std::string Str() const
177 s << int64_t(P) << "/" << int64_t(Q);
185 inline Rational<int64_t> pow(const Rational<int64_t> & a, const Rational<int64_t> & b)
187 //TODO:Implement properly
188 int64_t P = std::pow((double)a.P, b.ToDouble());
189 int64_t Q = std::pow((double)a.Q, b.ToDouble());
190 return Rational<int64_t>(P, Q);