/** * Return a new rational representing <code>this / other</code>. * * @param other Rational to divide. * @return Quotient of <code>this</code> and <code>other</code>. */ @Override public IFraction div(IFraction other) { if (other instanceof BigFractionSym) { return ((BigFractionSym) other).idiv(this); } FractionSym fs = (FractionSym) other; if (fs.fDenominator == 1) { if (fs.fNumerator == 1) { return this; } if (fs.fNumerator == -1) { return this.negate(); } } long newnum = (long) fNumerator * fs.fDenominator; long newdenom = (long) fDenominator * fs.fNumerator; // +-inf : -c = -+inf if (newdenom == 0 && fs.fNumerator < 0) newnum = -newnum; return valueOf(newnum, newdenom); }
/** * Return a new rational representing <code>this * other</code>. * * @param other big integer to multiply. * @return Product of <code>this</code> and <code>other</code>. */ public IFraction mul(BigInteger other) { if (other.bitLength() <= 31) { int oint = other.intValue(); if (oint == 1) return this; if (oint == -1) return this.negate(); long newnum = (long) fNumerator * oint; return valueOf(newnum, fDenominator); } if (this.isOne()) { return valueOf(other, BigInteger.ONE); } if (this.isMinusOne()) { return valueOf(other.negate(), BigInteger.ONE); } return valueOf(toBigNumerator().multiply(other), toBigDenominator()); }
@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()); }
/** * Return a new rational representing <code>this * other</code>. * * @param other Rational to multiply. * @return Product of <code>this</code> and <code>other</code>. */ @Override public IFraction mul(IFraction other) { if (other.isOne()) { return this; } if (this.isOne()) { return other; } if (other.isMinusOne()) { return this.negate(); } if (this.isMinusOne()) { return other.negate(); } if (other instanceof BigFractionSym) { return other.mul(this); } FractionSym fs = (FractionSym) other; long newnum = (long) fNumerator * fs.fNumerator; long newdenom = (long) fDenominator * fs.fDenominator; return valueOf(newnum, newdenom); }