/** {@inheritDoc} */ @Override public String toString() { try { StringBuilder sb = new StringBuilder(); OutputFormFactory.get().convertFraction(sb, toBigNumerator(), toBigDenominator(), Integer.MIN_VALUE, OutputFormFactory.NO_PLUS_CALL); return sb.toString(); } catch (Exception e1) { // fall back to simple output format return toBigNumerator().toString() + "/" + toBigDenominator().toString(); } }
@Override public IRational add(IRational parm1) { if (parm1.isZero()) { return this; } if (parm1 instanceof IFraction) { return add((IFraction) parm1); } if (parm1 instanceof IntegerSym) { IntegerSym is = (IntegerSym) parm1; long newnum = fNumerator + (long) fDenominator * (long) is.fIntValue; return valueOf(newnum, fDenominator); } BigInteger newnum = toBigNumerator().add(toBigDenominator().multiply(parm1.toBigNumerator())); return valueOf(newnum, toBigDenominator()); }
/** * 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()); }