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