Java Code Examples for org.apache.commons.math.util.MathUtils#gcd()

The following examples show how to use org.apache.commons.math.util.MathUtils#gcd() . 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</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 MathRuntimeException.createIllegalArgumentException(NULL_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 = 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));
}
 
Example 2
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</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 MathRuntimeException.createIllegalArgumentException(NULL_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 = 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));
}
 
Example 3
Source File: Math_91_Fraction_t.java    From coming with MIT License 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</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));
}
 
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</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 MathRuntimeException.createIllegalArgumentException("null 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 = 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));
}
 
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</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));
}
 
Example 6
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</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 MathRuntimeException.createIllegalArgumentException(NULL_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 = 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));
}
 
Example 7
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</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 MathRuntimeException.createIllegalArgumentException("null 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 = 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));
}
 
Example 8
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <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 MathRuntimeException.createArithmeticException(
                "zero denominator in fraction {0}/{1}",
                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 MathRuntimeException.createArithmeticException(
                    "overflow in fraction {0}/{1}, cannot negate",
                    numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = MathUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 9
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <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 MathRuntimeException.createArithmeticException(
                "zero denominator in fraction {0}/{1}",
                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 MathRuntimeException.createArithmeticException(
                    "overflow in fraction {0}/{1}, cannot negate",
                    numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = MathUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 10
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <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 MathRuntimeException.createArithmeticException(
              ZERO_DENOMINATOR_MESSAGE, 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 MathRuntimeException.createArithmeticException(
                  OVERFLOW_MESSAGE, numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = MathUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 11
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 = MathUtils.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</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 MathRuntimeException.createArithmeticException(
              ZERO_DENOMINATOR_MESSAGE, 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 MathRuntimeException.createArithmeticException(
                  OVERFLOW_MESSAGE, numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = MathUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 13
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 = MathUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 14
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 ArithmeticException if the denominator is <code>zero</code>
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw MathRuntimeException.createArithmeticException(
              ZERO_DENOMINATOR_MESSAGE, num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE || den == Integer.MIN_VALUE) {
            throw MathRuntimeException.createArithmeticException(
                  OVERFLOW_MESSAGE, num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = MathUtils.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 15
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 ArithmeticException if the denominator is <code>zero</code>
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw MathRuntimeException.createArithmeticException(
              ZERO_DENOMINATOR_MESSAGE, num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE || den == Integer.MIN_VALUE) {
            throw MathRuntimeException.createArithmeticException(
                  OVERFLOW_MESSAGE, num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = MathUtils.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: 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 ArithmeticException if the denominator is <code>zero</code>
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw MathRuntimeException.createArithmeticException("zero denominator in fraction {0}/{1}",
                                                             num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE || den == Integer.MIN_VALUE) {
            throw MathRuntimeException.createArithmeticException("overflow in fraction {0}/{1}, cannot negate",
                                                                 num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = MathUtils.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 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 ArithmeticException if the denominator is <code>zero</code>
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw MathRuntimeException.createArithmeticException("zero denominator in fraction {0}/{1}",
                                                             num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE || den == Integer.MIN_VALUE) {
            throw MathRuntimeException.createArithmeticException("overflow in fraction {0}/{1}, cannot negate",
                                                                 num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = MathUtils.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: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reduce this fraction to lowest terms.  This is accomplished by dividing
 * both numerator and denominator by their greatest common divisor.
 */
private void reduce() {
    // reduce numerator and denominator by greatest common denominator.
    int d = MathUtils.gcd(numerator, denominator);
    if (d > 1) {
        numerator /= d;
        denominator /= d;
    }

    // move sign to numerator.
    if (denominator < 0) {
        numerator *= -1;
        denominator *= -1;
    }
}
 
Example 19
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <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 MathRuntimeException.createArithmeticException(
              ZERO_DENOMINATOR_MESSAGE, 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 MathRuntimeException.createArithmeticException(
                  OVERFLOW_MESSAGE, numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = MathUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 20
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <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 MathRuntimeException.createArithmeticException(
                "zero denominator in fraction {0}/{1}",
                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 MathRuntimeException.createArithmeticException(
                    "overflow in fraction {0}/{1}, cannot negate",
                    numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = MathUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}