Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#FRACTION

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#FRACTION . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Fraction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <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}
 * @return a {@code Fraction} instance with the resulting values
 * @throws NullArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction multiply(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    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 = ArithmeticUtils.gcd(numerator, fraction.denominator);
    int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
    return getReducedFraction
    (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2),
            ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1));
}
 
Example 2
Source File: BigFraction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Adds the value of this fraction to another, returning the result in
 * reduced form.
 * </p>
 *
 * @param fraction
 *            the {@link BigFraction} to add, must not be <code>null</code>.
 * @return a {@link BigFraction} instance with the resulting values.
 * @throws NullArgumentException if the {@link BigFraction} is {@code null}.
 */
public BigFraction add(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (ZERO.equals(fraction)) {
        return this;
    }

    BigInteger num = null;
    BigInteger den = null;

    if (denominator.equals(fraction.denominator)) {
        num = numerator.add(fraction.numerator);
        den = denominator;
    } else {
        num = (numerator.multiply(fraction.denominator)).add((fraction.numerator).multiply(denominator));
        den = denominator.multiply(fraction.denominator);
    }
    return new BigFraction(num, den);

}
 
Example 3
Source File: Fraction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <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}
 * @return a {@code Fraction} instance with the resulting values
 * @throws NullArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction multiply(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    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 = ArithmeticUtils.gcd(numerator, fraction.denominator);
    int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
    return getReducedFraction
    (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2),
            ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1));
}
 
Example 4
Source File: Fraction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <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}
 * @return a {@code Fraction} instance with the resulting values
 * @throws NullArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction multiply(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    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 = ArithmeticUtils.gcd(numerator, fraction.denominator);
    int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
    return getReducedFraction
    (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2),
            ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1));
}
 
Example 5
Source File: Fraction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <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}
 * @return a {@code Fraction} instance with the resulting values
 * @throws NullArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction multiply(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    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 = ArithmeticUtils.gcd(numerator, fraction.denominator);
    int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
    return getReducedFraction
    (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2),
            ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1));
}
 
Example 6
Source File: 1_BigFraction.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Subtracts the value of another fraction from the value of this one,
 * returning the result in reduced form.
 * </p>
 *
 * @param fraction {@link BigFraction} to subtract, must not be {@code null}.
 * @return a {@link BigFraction} instance with the resulting values
 * @throws NullArgumentException if the {@code fraction} is {@code null}.
 */
public BigFraction subtract(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (ZERO.equals(fraction)) {
        return this;
    }

    BigInteger num = null;
    BigInteger den = null;
    if (denominator.equals(fraction.denominator)) {
        num = numerator.subtract(fraction.numerator);
        den = denominator;
    } else {
        num = (numerator.multiply(fraction.denominator)).subtract((fraction.numerator).multiply(denominator));
        den = denominator.multiply(fraction.denominator);
    }
    return new BigFraction(num, den);

}
 
Example 7
Source File: BigFraction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Subtracts the value of another fraction from the value of this one,
 * returning the result in reduced form.
 * </p>
 *
 * @param fraction {@link BigFraction} to subtract, must not be {@code null}.
 * @return a {@link BigFraction} instance with the resulting values
 * @throws NullArgumentException if the {@code fraction} is {@code null}.
 */
public BigFraction subtract(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (ZERO.equals(fraction)) {
        return this;
    }

    BigInteger num = null;
    BigInteger den = null;
    if (denominator.equals(fraction.denominator)) {
        num = numerator.subtract(fraction.numerator);
        den = denominator;
    } else {
        num = (numerator.multiply(fraction.denominator)).subtract((fraction.numerator).multiply(denominator));
        den = denominator.multiply(fraction.denominator);
    }
    return new BigFraction(num, den);

}
 
Example 8
Source File: BigFraction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Subtracts the value of another fraction from the value of this one,
 * returning the result in reduced form.
 * </p>
 *
 * @param fraction {@link BigFraction} to subtract, must not be {@code null}.
 * @return a {@link BigFraction} instance with the resulting values
 * @throws NullArgumentException if the {@code fraction} is {@code null}.
 */
public BigFraction subtract(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (ZERO.equals(fraction)) {
        return this;
    }

    BigInteger num = null;
    BigInteger den = null;
    if (denominator.equals(fraction.denominator)) {
        num = numerator.subtract(fraction.numerator);
        den = denominator;
    } else {
        num = (numerator.multiply(fraction.denominator)).subtract((fraction.numerator).multiply(denominator));
        den = denominator.multiply(fraction.denominator);
    }
    return new BigFraction(num, den);

}
 
Example 9
Source File: 1_BigFraction.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Subtracts the value of another fraction from the value of this one,
 * returning the result in reduced form.
 * </p>
 *
 * @param fraction {@link BigFraction} to subtract, must not be {@code null}.
 * @return a {@link BigFraction} instance with the resulting values
 * @throws NullArgumentException if the {@code fraction} is {@code null}.
 */
public BigFraction subtract(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (ZERO.equals(fraction)) {
        return this;
    }

    BigInteger num = null;
    BigInteger den = null;
    if (denominator.equals(fraction.denominator)) {
        num = numerator.subtract(fraction.numerator);
        den = denominator;
    } else {
        num = (numerator.multiply(fraction.denominator)).subtract((fraction.numerator).multiply(denominator));
        den = denominator.multiply(fraction.denominator);
    }
    return new BigFraction(num, den);

}
 
Example 10
Source File: Fraction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <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}
 * @return a {@code Fraction} instance with the resulting values
 * @throws NullArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction multiply(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    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 = ArithmeticUtils.gcd(numerator, fraction.denominator);
    int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
    return getReducedFraction
    (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2),
            ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1));
}
 
Example 11
Source File: BigFraction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Subtracts the value of another fraction from the value of this one,
 * returning the result in reduced form.
 * </p>
 *
 * @param fraction {@link BigFraction} to subtract, must not be {@code null}.
 * @return a {@link BigFraction} instance with the resulting values
 * @throws NullArgumentException if the {@code fraction} is {@code null}.
 */
public BigFraction subtract(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (ZERO.equals(fraction)) {
        return this;
    }

    BigInteger num = null;
    BigInteger den = null;
    if (denominator.equals(fraction.denominator)) {
        num = numerator.subtract(fraction.numerator);
        den = denominator;
    } else {
        num = (numerator.multiply(fraction.denominator)).subtract((fraction.numerator).multiply(denominator));
        den = denominator.multiply(fraction.denominator);
    }
    return new BigFraction(num, den);

}
 
Example 12
Source File: BigFraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>
 * Multiplies the value of this fraction by another, returning the result in
 * reduced form.
 * </p>
 *
 * @param fraction Fraction to multiply by, must not be {@code null}.
 * @return a {@link BigFraction} instance with the resulting values.
 * @throws NullArgumentException if {@code fraction} is {@code null}.
 */
public BigFraction multiply(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (numerator.equals(BigInteger.ZERO) ||
        fraction.numerator.equals(BigInteger.ZERO)) {
        return ZERO;
    }
    return new BigFraction(numerator.multiply(fraction.numerator),
                           denominator.multiply(fraction.denominator));
}
 
Example 13
Source File: Math_27_Fraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Divide the value of this fraction by another.</p>
 *
 * @param fraction  the fraction to divide by, must not be {@code null}
 * @return a {@code Fraction} instance with the resulting values
 * @throws IllegalArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the fraction to divide by is zero
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction divide(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (fraction.numerator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_FRACTION_TO_DIVIDE_BY,
                                          fraction.numerator, fraction.denominator);
    }
    return multiply(fraction.reciprocal());
}
 
Example 14
Source File: Math_26_Fraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Divide the value of this fraction by another.</p>
 *
 * @param fraction  the fraction to divide by, must not be {@code null}
 * @return a {@code Fraction} instance with the resulting values
 * @throws IllegalArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the fraction to divide by is zero
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction divide(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (fraction.numerator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_FRACTION_TO_DIVIDE_BY,
                                          fraction.numerator, fraction.denominator);
    }
    return multiply(fraction.reciprocal());
}
 
Example 15
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Divide the value of this fraction by another.</p>
 *
 * @param fraction  the fraction to divide by, must not be {@code null}
 * @return a {@code Fraction} instance with the resulting values
 * @throws IllegalArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the fraction to divide by is zero
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction divide(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (fraction.numerator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_FRACTION_TO_DIVIDE_BY,
                                          fraction.numerator, fraction.denominator);
    }
    return multiply(fraction.reciprocal());
}
 
Example 16
Source File: BigFraction_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * <p>
 * Divide the value of this fraction by another, returning the result in
 * reduced form.
 * </p>
 *
 * @param fraction Fraction to divide by, must not be {@code null}.
 * @return a {@link BigFraction} instance with the resulting values.
 * @throws NullArgumentException if the {@code fraction} is {@code null}.
 * @throws MathArithmeticException if the fraction to divide by is zero
 */
public BigFraction divide(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (BigInteger.ZERO.equals(fraction.numerator)) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
    }

    return multiply(fraction.reciprocal());
}
 
Example 17
Source File: 1_BigFraction.java    From SimFix with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>
 * Divide the value of this fraction by another, returning the result in
 * reduced form.
 * </p>
 *
 * @param fraction Fraction to divide by, must not be {@code null}.
 * @return a {@link BigFraction} instance with the resulting values.
 * @throws NullArgumentException if the {@code fraction} is {@code null}.
 * @throws MathArithmeticException if the fraction to divide by is zero
 */
public BigFraction divide(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (BigInteger.ZERO.equals(fraction.numerator)) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
    }

    return multiply(fraction.reciprocal());
}
 
Example 18
Source File: BigFraction.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>
 * Divide the value of this fraction by another, returning the result in
 * reduced form.
 * </p>
 *
 * @param fraction Fraction to divide by, must not be {@code null}.
 * @return a {@link BigFraction} instance with the resulting values.
 * @throws NullArgumentException if the {@code fraction} is {@code null}.
 * @throws MathArithmeticException if the fraction to divide by is zero
 */
public BigFraction divide(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (BigInteger.ZERO.equals(fraction.numerator)) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
    }

    return multiply(fraction.reciprocal());
}
 
Example 19
Source File: BigFraction.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>
 * Divide the value of this fraction by another, returning the result in
 * reduced form.
 * </p>
 *
 * @param fraction Fraction to divide by, must not be {@code null}.
 * @return a {@link BigFraction} instance with the resulting values.
 * @throws NullArgumentException if the {@code fraction} is {@code null}.
 * @throws MathArithmeticException if the fraction to divide by is zero
 */
public BigFraction divide(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (BigInteger.ZERO.equals(fraction.numerator)) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
    }

    return multiply(fraction.reciprocal());
}
 
Example 20
Source File: BigFraction.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>
 * Divide the value of this fraction by another, returning the result in
 * reduced form.
 * </p>
 *
 * @param fraction Fraction to divide by, must not be {@code null}.
 * @return a {@link BigFraction} instance with the resulting values.
 * @throws NullArgumentException if the {@code fraction} is {@code null}.
 * @throws MathArithmeticException if the fraction to divide by is zero
 */
public BigFraction divide(final BigFraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (BigInteger.ZERO.equals(fraction.numerator)) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
    }

    return multiply(fraction.reciprocal());
}