/** * Compares the value of this with another constant. * * @param val The other constant to compare with * @return The arithmetic maximum of this and val. */ public Rational max(final Rational val) { if (compareTo(val) > 0) { return this; } else { return val; } } /* Rational.max */
/** * Compares the value of this with another constant. * * @param val The other constant to compare with * @return The arithmetic minimum of this and val. */ public Rational min(final Rational val) { if (compareTo(val) < 0) { return this; } else { return val; } } /* Rational.min */
/** * Compares the value of this with another constant. * * @param val the other constant to compare with * @return -1, 0 or 1 if this number is numerically less than, equal to, * or greater than val. */ public int compareTo(final BigInteger val) { final Rational val2 = new Rational(val, BigInteger.ONE); return (compareTo(val2)); } /* Rational.compareTo */
/** * Multiply and round. * * @param x The left factor. * @param f The right factor. * @return The product x*f. */ static public BigDecimal multiplyRound(final BigDecimal x, final Rational f) { if (f.compareTo(BigInteger.ZERO) == 0) { return BigDecimal.ZERO; } else { /* Convert the rational value with two digits of extra precision */ MathContext mc = new MathContext(2 + x.precision()); BigDecimal fbd = f.BigDecimalValue(mc); /* and the precision of the product is then dominated by the precision in x */ return multiplyRound(x, fbd); } }
/** * The natural logarithm. * * @param r The main argument, a strictly positive value. * @param mc The requirements on the precision. * @return ln(r). */ static public BigDecimal log(final Rational r, final MathContext mc) { /* the value is undefined if x is negative. */ if (r.compareTo(Rational.ZERO) <= 0) { throw new ArithmeticException("Cannot take log of negative " + r.toString()); } else if (r.compareTo(Rational.ONE) == 0) { return BigDecimal.ZERO; } else { /* log(r+epsr) = log(r)+epsr/r. Convert the precision to an absolute error in the result. * eps contains the required absolute error of the result, epsr/r. */ double eps = prec2err(Math.log(r.doubleValue()), mc.getPrecision()); /* Convert this further into a requirement of the relative precision in r, given that * epsr/r is also the relative precision of r. Add one safety digit. */ MathContext mcloc = new MathContext(1 + err2prec(eps)); final BigDecimal resul = log(r.BigDecimalValue(mcloc)); return resul.round(mc); } } /* log */
/** * Compares the value of this with another constant. * * @param val The other constant to compare with * @return The arithmetic maximum of this and val. */ public Rational max(final Rational val) { if (compareTo(val) > 0) { return this; } else { return val; } } /* Rational.max */
/** * Compares the value of this with another constant. * * @param val The other constant to compare with * @return The arithmetic minimum of this and val. */ public Rational min(final Rational val) { if (compareTo(val) < 0) { return this; } else { return val; } } /* Rational.min */
/** * Compares the value of this with another constant. * * @param val the other constant to compare with * @return -1, 0 or 1 if this number is numerically less than, equal to, * or greater than val. */ public int compareTo(final BigInteger val) { final Rational val2 = new Rational(val, BigInteger.ONE); return (compareTo(val2)); } /* Rational.compareTo */
/** * Multiply and round. * * @param x The left factor. * @param f The right factor. * @return The product x*f. */ static public BigDecimal multiplyRound(final BigDecimal x, final Rational f) { if (f.compareTo(BigInteger.ZERO) == 0) { return BigDecimal.ZERO; } else { /* Convert the rational value with two digits of extra precision */ MathContext mc = new MathContext(2 + x.precision()); BigDecimal fbd = f.BigDecimalValue(mc); /* and the precision of the product is then dominated by the precision in x */ return multiplyRound(x, fbd); } }
/** * The natural logarithm. * * @param r The main argument, a strictly positive value. * @param mc The requirements on the precision. * @return ln(r). */ static public BigDecimal log(final Rational r, final MathContext mc) { /* the value is undefined if x is negative. */ if (r.compareTo(Rational.ZERO) <= 0) { throw new ArithmeticException("Cannot take log of negative " + r.toString()); } else if (r.compareTo(Rational.ONE) == 0) { return BigDecimal.ZERO; } else { /* log(r+epsr) = log(r)+epsr/r. Convert the precision to an absolute error in the result. * eps contains the required absolute error of the result, epsr/r. */ double eps = prec2err(Math.log(r.doubleValue()), mc.getPrecision()); /* Convert this further into a requirement of the relative precision in r, given that * epsr/r is also the relative precision of r. Add one safety digit. */ MathContext mcloc = new MathContext(1 + err2prec(eps)); final BigDecimal resul = log(r.BigDecimalValue(mcloc)); return resul.round(mc); } } /* log */