int gcd = greatestCommonDivisor(numerator, denominator); numerator /= gcd; denominator /= gcd;
/** * <p>Reduce the fraction to the smallest values for the numerator and * denominator, returning the result.</p> * * <p>For example, if this fraction represents 2/4, then the result * will be 1/2.</p> * * @return a new reduced fraction instance, or this if no simplification possible */ public Fraction reduce() { if (numerator == 0) { return equals(ZERO) ? this : ZERO; } int gcd = greatestCommonDivisor(Math.abs(numerator), denominator); if (gcd == 1) { return this; } return Fraction.getFraction(numerator / gcd, denominator / gcd); }
/** * <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 multiplyBy(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 = greatestCommonDivisor(numerator, fraction.denominator); int d2 = greatestCommonDivisor(fraction.numerator, denominator); return getReducedFraction (mulAndCheck(numerator/d1, fraction.numerator/d2), mulPosAndCheck(denominator/d2, fraction.denominator/d1)); }
int d1 = greatestCommonDivisor(denominator, fraction.denominator); if (d1==1) { int d2 = (tmodd1==0)?d1:greatestCommonDivisor(tmodd1, d1);
int gcd = greatestCommonDivisor(numerator, denominator); numerator /= gcd; denominator /= gcd;
int gcd = greatestCommonDivisor(numerator, denominator); numerator /= gcd; denominator /= gcd;
int gcd = greatestCommonDivisor(numerator, denominator); numerator /= gcd; denominator /= gcd;
/** * <p>Reduce the fraction to the smallest values for the numerator and * denominator, returning the result.</p> * * <p>For example, if this fraction represents 2/4, then the result * will be 1/2.</p> * * @return a new reduced fraction instance, or this if no simplification possible */ public Fraction reduce() { if (numerator == 0) { return equals(ZERO) ? this : ZERO; } int gcd = greatestCommonDivisor(Math.abs(numerator), denominator); if (gcd == 1) { return this; } return Fraction.getFraction(numerator / gcd, denominator / gcd); }
int gcd = greatestCommonDivisor(numerator, denominator); numerator /= gcd; denominator /= gcd;
int gcd = greatestCommonDivisor(numerator, denominator); numerator /= gcd; denominator /= gcd;
/** * <p>Reduce the fraction to the smallest values for the numerator and * denominator, returning the result.</p> * * <p>For example, if this fraction represents 2/4, then the result * will be 1/2.</p> * * @return a new reduced fraction instance, or this if no simplification possible */ public Fraction reduce() { if (numerator == 0) { return equals(ZERO) ? this : ZERO; } int gcd = greatestCommonDivisor(Math.abs(numerator), denominator); if (gcd == 1) { return this; } return Fraction.getFraction(numerator / gcd, denominator / gcd); }
/** * <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 multiplyBy(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 = greatestCommonDivisor(numerator, fraction.denominator); int d2 = greatestCommonDivisor(fraction.numerator, denominator); return getReducedFraction (mulAndCheck(numerator/d1, fraction.numerator/d2), mulPosAndCheck(denominator/d2, fraction.denominator/d1)); }
/** * <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 multiplyBy(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 = greatestCommonDivisor(numerator, fraction.denominator); int d2 = greatestCommonDivisor(fraction.numerator, denominator); return getReducedFraction (mulAndCheck(numerator/d1, fraction.numerator/d2), mulPosAndCheck(denominator/d2, fraction.denominator/d1)); }
/** * <p>Reduce the fraction to the smallest values for the numerator and * denominator, returning the result.</p> * * <p>For example, if this fraction represents 2/4, then the result * will be 1/2.</p> * * @return a new reduced fraction instance, or this if no simplification possible */ public Fraction reduce() { if (numerator == 0) { return equals(ZERO) ? this : ZERO; } int gcd = greatestCommonDivisor(Math.abs(numerator), denominator); if (gcd == 1) { return this; } return Fraction.getFraction(numerator / gcd, denominator / gcd); }
/** * <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 multiplyBy(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 = greatestCommonDivisor(numerator, fraction.denominator); int d2 = greatestCommonDivisor(fraction.numerator, denominator); return getReducedFraction (mulAndCheck(numerator/d1, fraction.numerator/d2), mulPosAndCheck(denominator/d2, fraction.denominator/d1)); }
/** * <p>Reduce the fraction to the smallest values for the numerator and * denominator, returning the result.</p> * * <p>For example, if this fraction represents 2/4, then the result * will be 1/2.</p> * * @return a new reduced fraction instance, or this if no simplification possible */ public Fraction reduce() { if (numerator == 0) { return equals(ZERO) ? this : ZERO; } int gcd = greatestCommonDivisor(Math.abs(numerator), denominator); if (gcd == 1) { return this; } return Fraction.getFraction(numerator / gcd, denominator / gcd); }
/** * <p>Reduce the fraction to the smallest values for the numerator and * denominator, returning the result.</p> * * <p>For example, if this fraction represents 2/4, then the result * will be 1/2.</p> * * @return a new reduced fraction instance, or this if no simplification possible */ public Fraction reduce() { if (numerator == 0) { return equals(ZERO) ? this : ZERO; } int gcd = greatestCommonDivisor(Math.abs(numerator), denominator); if (gcd == 1) { return this; } return Fraction.getFraction(numerator / gcd, denominator / gcd); }
/** * <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 multiplyBy(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 = greatestCommonDivisor(numerator, fraction.denominator); int d2 = greatestCommonDivisor(fraction.numerator, denominator); return getReducedFraction (mulAndCheck(numerator/d1, fraction.numerator/d2), mulPosAndCheck(denominator/d2, fraction.denominator/d1)); }
/** * <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 multiplyBy(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 = greatestCommonDivisor(numerator, fraction.denominator); int d2 = greatestCommonDivisor(fraction.numerator, denominator); return getReducedFraction (mulAndCheck(numerator/d1, fraction.numerator/d2), mulPosAndCheck(denominator/d2, fraction.denominator/d1)); }
int d1 = greatestCommonDivisor(denominator, fraction.denominator); if (d1==1) { int d2 = (tmodd1==0)?d1:greatestCommonDivisor(tmodd1, d1);