⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fraction.java

📁 Apache的common math数学软件包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }        /**     * Test for the equality of two fractions.  If the lowest term     * numerator and denominators are the same for both fractions, the two     * fractions are considered to be equal.     * @param other fraction to test for equality to this fraction     * @return true if two fractions are equal, false if object is     *         <tt>null</tt>, not an instance of {@link Fraction}, or not equal     *         to this fraction instance.     */    public boolean equals(Object other) {        boolean ret;                if (this == other) {             ret = true;        } else if (other == null) {            ret = false;        } else {            try {                // since fractions are always in lowest terms, numerators and                // denominators can be compared directly for equality.                Fraction rhs = (Fraction)other;                ret = (numerator == rhs.numerator) &&                    (denominator == rhs.denominator);            } catch (ClassCastException ex) {                // ignore exception                ret = false;            }        }                return ret;    }        /**     * Gets the fraction as a <tt>float</tt>. This calculates the fraction as     * the numerator divided by denominator.     * @return the fraction as a <tt>float</tt>     */    public float floatValue() {        return (float)doubleValue();    }        /**     * Access the denominator.     * @return the denominator.     */    public int getDenominator() {        return denominator;    }        /**     * Access the numerator.     * @return the numerator.     */    public int getNumerator() {        return numerator;    }        /**     * Gets a hashCode for the fraction.     * @return a hash code value for this object     */    public int hashCode() {        return 37 * (37 * 17 + getNumerator()) + getDenominator();    }        /**     * Gets the fraction as an <tt>int</tt>. This returns the whole number part     * of the fraction.     * @return the whole number fraction part     */    public int intValue() {        return (int)doubleValue();    }        /**     * Gets the fraction as a <tt>long</tt>. This returns the whole number part     * of the fraction.     * @return the whole number fraction part     */    public long longValue() {        return (long)doubleValue();    }        /**     * Return the additive inverse of this fraction.     * @return the negation of this fraction.     */    public Fraction negate() {        if (numerator==Integer.MIN_VALUE) {            throw new ArithmeticException("overflow: too large to negate");        }        return new Fraction(-numerator, denominator);    }    /**     * Return the multiplicative inverse of this fraction.     * @return the reciprocal fraction     */    public Fraction reciprocal() {        return new Fraction(denominator, numerator);    }        /**     * <p>Adds the value of this fraction to another, returning the result in reduced form.     * The algorithm follows Knuth, 4.5.1.</p>     *     * @param fraction  the fraction to add, must not be <code>null</code>     * @return a <code>Fraction</code> instance with the resulting values     * @throws IllegalArgumentException if the fraction is <code>null</code>     * @throws ArithmeticException if the resulting numerator or denominator exceeds     *  <code>Integer.MAX_VALUE</code>     */    public Fraction add(Fraction fraction) {        return addSub(fraction, true /* add */);    }    /**     * <p>Subtracts the value of another fraction from the value of this one,      * returning the result in reduced form.</p>     *     * @param fraction  the fraction to subtract, must not be <code>null</code>     * @return a <code>Fraction</code> instance with the resulting values     * @throws IllegalArgumentException if the fraction is <code>null</code>     * @throws ArithmeticException if the resulting numerator or denominator     *   cannot be represented in an <code>int</code>.     */    public Fraction subtract(Fraction fraction) {        return addSub(fraction, false /* subtract */);    }    /**      * Implement add and subtract using algorithm described in Knuth 4.5.1.     *      * @param fraction the fraction to subtract, must not be <code>null</code>     * @param isAdd true to add, false to subtract     * @return a <code>Fraction</code> instance with the resulting values     * @throws IllegalArgumentException if the fraction is <code>null</code>     * @throws ArithmeticException if the resulting numerator or denominator     *   cannot be represented in an <code>int</code>.     */    private Fraction addSub(Fraction fraction, boolean isAdd) {        if (fraction == null) {            throw new IllegalArgumentException("The fraction must not be null");        }        // zero is identity for addition.        if (numerator == 0) {            return isAdd ? fraction : fraction.negate();        }        if (fraction.numerator == 0) {            return this;        }             // if denominators are randomly distributed, d1 will be 1 about 61%        // of the time.        int d1 = MathUtils.gcd(denominator, fraction.denominator);        if (d1==1) {            // result is ( (u*v' +/- u'v) / u'v')            int uvp = MathUtils.mulAndCheck(numerator, fraction.denominator);            int upv = MathUtils.mulAndCheck(fraction.numerator, denominator);            return new Fraction                (isAdd ? MathUtils.addAndCheck(uvp, upv) :                  MathUtils.subAndCheck(uvp, upv),                 MathUtils.mulAndCheck(denominator, fraction.denominator));        }        // the quantity 't' requires 65 bits of precision; see knuth 4.5.1        // exercise 7.  we're going to use a BigInteger.        // t = u(v'/d1) +/- v(u'/d1)        BigInteger uvp = BigInteger.valueOf(numerator)        .multiply(BigInteger.valueOf(fraction.denominator/d1));        BigInteger upv = BigInteger.valueOf(fraction.numerator)        .multiply(BigInteger.valueOf(denominator/d1));        BigInteger t = isAdd ? uvp.add(upv) : uvp.subtract(upv);        // but d2 doesn't need extra precision because        // d2 = gcd(t,d1) = gcd(t mod d1, d1)        int tmodd1 = t.mod(BigInteger.valueOf(d1)).intValue();        int d2 = (tmodd1==0)?d1:MathUtils.gcd(tmodd1, d1);        // result is (t/d2) / (u'/d1)(v'/d2)        BigInteger w = t.divide(BigInteger.valueOf(d2));        if (w.bitLength() > 31) {            throw new ArithmeticException            ("overflow: numerator too large after multiply");        }        return new Fraction (w.intValue(),                 MathUtils.mulAndCheck(denominator/d1,                         fraction.denominator/d2));    }    /**     * <p>Multiplies the value of this fraction by another, returning the      * result in reduced form.</p>     *     * @param fraction  the fraction to multiply by, must not be <code>null</code>     * @return a <code>Fraction</code> instance with the resulting values     * @throws IllegalArgumentException if the fraction is <code>null</code>     * @throws ArithmeticException if the resulting numerator or denominator exceeds     *  <code>Integer.MAX_VALUE</code>     */    public Fraction multiply(Fraction fraction) {        if (fraction == null) {            throw new IllegalArgumentException("The fraction must not be null");        }        if (numerator == 0 || fraction.numerator == 0) {            return ZERO;        }        // knuth 4.5.1        // make sure we don't overflow unless the result *must* overflow.        int d1 = MathUtils.gcd(numerator, fraction.denominator);        int d2 = MathUtils.gcd(fraction.numerator, denominator);        return getReducedFraction        (MathUtils.mulAndCheck(numerator/d1, fraction.numerator/d2),                MathUtils.mulAndCheck(denominator/d2, fraction.denominator/d1));    }    /**     * <p>Divide the value of this fraction by another.</p>     *     * @param fraction  the fraction to divide by, must not be <code>null</code>     * @return a <code>Fraction</code> instance with the resulting values     * @throws IllegalArgumentException if the fraction is <code>null</code>     * @throws ArithmeticException if the fraction to divide by is zero     * @throws ArithmeticException if the resulting numerator or denominator exceeds     *  <code>Integer.MAX_VALUE</code>     */    public Fraction divide(Fraction fraction) {        if (fraction == null) {            throw new IllegalArgumentException("The fraction must not be null");        }        if (fraction.numerator == 0) {            throw new ArithmeticException("The fraction to divide by must not be zero");        }        return multiply(fraction.reciprocal());    }        /**     * <p>Creates a <code>Fraction</code> instance with the 2 parts     * of a fraction Y/Z.</p>     *     * <p>Any negative signs are resolved to be on the numerator.</p>     *     * @param numerator  the numerator, for example the three in 'three sevenths'     * @param denominator  the denominator, for example the seven in 'three sevenths'     * @return a new fraction instance, with the numerator and denominator reduced     * @throws ArithmeticException if the denominator is <code>zero</code>     */    public static Fraction getReducedFraction(int numerator, int denominator) {        if (denominator == 0) {            throw new ArithmeticException("The denominator must not be zero");        }        if (numerator==0) {            return ZERO; // normalize zero.        }        // allow 2^k/-2^31 as a valid fraction (where k>0)        if (denominator==Integer.MIN_VALUE && (numerator&1)==0) {            numerator/=2; denominator/=2;        }        if (denominator < 0) {            if (numerator==Integer.MIN_VALUE ||                    denominator==Integer.MIN_VALUE) {                throw new ArithmeticException("overflow: can't negate");            }            numerator = -numerator;            denominator = -denominator;        }        // simplify fraction.        int gcd = MathUtils.gcd(numerator, denominator);        numerator /= gcd;        denominator /= gcd;        return new Fraction(numerator, denominator);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -