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

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#ZERO_DENOMINATOR_IN_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: Math_26_Fraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 2
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} 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 MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    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 MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 3
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 4
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} 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 MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    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 MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 5
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 6
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} 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 MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    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 MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 7
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} 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 MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    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 MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 8
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 9
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 10
Source File: Math_26_Fraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} 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 MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    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 MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 11
Source File: Math_1_Fraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 12
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} 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 MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    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 MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 13
Source File: Math_26_Fraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 14
Source File: Math_27_Fraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} 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 MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    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 MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 15
Source File: Math_27_Fraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 16
Source File: Math_27_Fraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} 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 MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    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 MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 17
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 18
Source File: Math_1_Fraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} 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 MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    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 MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 19
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 20
Source File: Math_1_Fraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} 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 MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    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 MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}