public static Rational sum(Iterable<Rational> list) { Rational sum = ZERO; for (Rational rat : list) { sum.addEq(rat); } return sum; }
public Rational add(Rational b) { if (zero(num)) return b; else if (zero(b.num)) return this; Rational rv = new Rational(this); rv.addEq(b); return rv; }
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(long b) { if (b == 0) return this; Rational c = new Rational(-b); c.addEq(this); return c; }
public Rational subtract(Rational b) { if (zero(b.num)) return this; Rational c = b.negate(); if (!zero(num)) c.addEq(this); return c; }
public Rational add(long b) { if (b == 0) return this; Rational rv = new Rational(b); rv.addEq(this); return rv; }