/** {@inheritDoc} */ @Override public IRational normalize() { if (fDenominator == 1) { return F.integer(fNumerator); } if (isZero()) { return F.C0; } return this; }
/** * Compute the gcd of two rationals (this and other). The gcd is the rational number, such that dividing this and other with the gcd * will yield two co-prime integers. * * @param other the second rational argument. * @return the gcd of this and other. */ public IFraction gcd(IFraction other) { if (isZero()) { return other; } if (other.isZero()) { return this; } if (other instanceof BigFractionSym) { return ((BigFractionSym) other).gcd(this); } /* new numerator = gcd(num, other.num) */ /* new denominator = lcm(denom, other.denom) */ FractionSym fs = (FractionSym) other; int gcddenom = ArithmeticUtils.gcd(fDenominator, fs.fDenominator); long denom = ((long) (fDenominator / gcddenom)) * (long) fs.fDenominator; long num = ArithmeticUtils.gcd(fNumerator < 0 ? -fNumerator : fNumerator, fs.fNumerator < 0 ? -fs.fNumerator : fs.fNumerator); return valueOf(num, denom); }
if (isZero()) { return other;
/** {@inheritDoc} */ public ISignedNumber minus(ISignedNumber that) { if (that instanceof FractionSym) { return this.add((FractionSym) that.negate()); } if (isZero()) { return that.negate(); } if (that instanceof IntegerSym) { return this.minus(valueOf(((IntegerSym) that).fInteger)); } return Num.valueOf(doubleValue() - that.doubleValue()); }
@Override public IRational multiply(IRational parm1) { if (isZero() || parm1.isZero()) { return F.C0; } if (parm1.isOne()) { return this; } if (parm1.isMinusOne()) { return this.negate(); } if (parm1 instanceof IFraction) { return mul((IFraction) parm1); } if (parm1 instanceof IntegerSym) { IntegerSym is = (IntegerSym) parm1; long newnum = (long) fNumerator * (long) is.fIntValue; return valueOf(newnum, fDenominator); } BigIntegerSym p1 = (BigIntegerSym) parm1; BigInteger newnum = toBigNumerator().multiply(p1.toBigNumerator()); return valueOf(newnum, toBigDenominator()); }