5 * A really shoddy implementation of Rational numbers
15 /* Recursive version of GCD
17 T gcd(const T & a, const T & b)
19 if (a == 1 || a == 0) return 1;
23 if (a > b) return gcd(a-b,b);
28 /** Greatest Common Divisor of p and q **/
30 T gcd(const T & p, const T & q)
42 while ((g = big % small) > 0)
50 template <class T = int64_t>
53 /** Construct from a double.**/
54 Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
57 CheckAccuracy(d, "Construct from double");
60 Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
65 Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
82 T g = gcd(llabs(P),llabs(Q));
87 bool operator==(const Rational & r) const
89 if (P == r.P && Q == r.Q) return true;
90 return ToDouble() == r.ToDouble();
94 bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
95 bool operator>(const Rational & r) const {return !(*this < r);}
96 bool operator<=(const Rational & r) const {return *this == r || *this < r;}
97 bool operator>=(const Rational & r) const {return *this == r || *this > r;}
98 bool operator!=(const Rational & r) const {return !(*this == r);}
100 Rational operator+(const Rational & r) const
102 Rational result = (r.P == 0) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
103 result.CheckAccuracy(ToDouble() + r.ToDouble(),"+");
106 Rational operator-(const Rational & r) const
108 Rational result = (r.P == 0) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
109 result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
112 Rational operator*(const Rational & r) const
114 Rational result(P * r.P, Q * r.Q);
115 if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
117 Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
121 Rational operator/(const Rational & r) const
123 Rational result(P * r.Q, Q*r.P);
124 if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
126 Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
131 /** To cheat, use these **/
132 //Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());}
133 //Rational operator-(const Rational & r) const {return Rational(ToDouble()-r.ToDouble());}
134 //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());}
135 //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
137 Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; return *this;}
138 Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
139 Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
140 Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
141 Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
143 double ToDouble() const {return (double)(P) / (double)(Q);}
144 bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
146 double result = fabs(ToDouble() - d) / d;
147 if (result > threshold)
149 Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
154 std::string Str() const
157 s << (int64_t)P << "/" << (int64_t)Q;
165 inline Rational<int64_t> pow(const Rational<int64_t> & a, const Rational<int64_t> & b)
167 //TODO:Implement properly
168 int64_t P = std::pow((double)a.P, b.ToDouble());
169 int64_t Q = std::pow((double)a.Q, b.ToDouble());
170 return Rational<int64_t>(P, Q);