const Rational Rational::operator-(const Rational &r) const { // Do division first to reduce chance of integer overflow errors. int lcm, pnew; lcm = r.GetQ()/gcd(this->den, r.GetQ()); lcm *= this->den; pnew = this->num*(lcm/this->den); pnew -= r.GetP()*(lcm/r.GetQ()); return Rational(pnew, lcm); }
const Rational Rational::operator*(const Rational &r) const { // First remove common divisors of opposing numerators and denominators! Reduce the chance of overflow error. // Multiplication is (a/b) * (p/q) int a=this->num, b=this->den, p=r.GetP(), q=r.GetQ(), g; g = gcd(a,q); a/=g; q/=g; g = gcd(b, p); b/=g; p/=g; return Rational(a*p, b*q); }