public int compareTo(Rational other) { if (num.equals(other.num) && den.equals(other.den)) return 0; // see if it is a num only compare... if (den.equals(other.den)) return (greater(other.num, this.num)) ? -1 : 1; // check signs... if ((zero(num) || negative(num)) && positive(other.num)) return -1; else if (positive(num) && (zero(other.num) || negative(other.num))) return 1; BigInteger c1 = new BigInteger(other.num.toString()); BigInteger c2 = new BigInteger(other.den.toString()); Rational c = new Rational(c1, c2); c = c.negate(); c.addEq(this); return (c.isZero() ? 0 : (negative(c.num) ? -1 : 1)); }
public Rational subtract(Rational b) { if (zero(b.num)) return this; Rational c = b.negate(); if (!zero(num)) c.addEq(this); return c; }