예제 #1
0
파일: Rational.java 프로젝트: vvirag/gte
  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));
  }
예제 #2
0
파일: Rational.java 프로젝트: vvirag/gte
 public Rational subtract(Rational b) {
   if (zero(b.num)) return this;
   Rational c = b.negate();
   if (!zero(num)) c.addEq(this);
   return c;
 }