bool lessThan(fraction x, fraction y) // non-member function returning a bool { // indicating if x < y int xDen = x.getDenom(); int yDen = y.getDenom(); int lcm = LCM(xDen, yDen); // same code as add, mostly int factor1 = lcm / xDen; int factor2 = lcm / yDen; return ((x.getNumer() * factor1) < (y.getNumer() * factor2)); }
fraction add(fraction x, fraction y) // non-member function to return a new { // fraction containing the sum of two others int xDen = x.getDenom(); int yDen = y.getDenom(); int lcm = LCM(xDen, yDen); // it would be trivial to implement int factor1 = lcm / xDen; // reduceMe here if desired. int factor2 = lcm / yDen; int num = (x.getNumer() * factor1) + (y.getNumer() * factor2); fraction z = fraction(num, lcm); return z; }
bool operator<(const fraction &a, const fraction &b) { double ax, ay; int as; getFrSpec(a.getNumer(), a.getDenom(), &ax, &ay, &as); if(as == -1) { ax =- ax; ay =-ay; } double bx, by; int bs; getFrSpec(b.getNumer(), b.getDenom(), &bx, &by, &bs); if(bs == -1) { bx =- bx; by =-by; } return (ax < bx || (ax == bx && ay < by)); }
const fraction operator*(const fraction &a, int64_t b) { if(b < a.getInterimMultOverflowPt()) { // b < interimMultOverflowPt return fraction(b * a.getNumer(), a.getDenom()); } else if(b <= a.getFinalMultOverflowPt()) { // interimMultOverflowPt <= b <= finalMultInterimPt cerr << "fraction::operator*- an interim overflow has occurred\n"; return fraction(I64_MAX); } else { // finalMultInterimPt < b cerr << "fraction::operator*- a final overflow has occurred\n"; return fraction(I64_MAX); } }