/** * Returns the least common multiple between two integer values. * * @param a the first integer value. * @param b the second integer value. * @return the least common multiple between a and b. * @throws ArithmeticException if the lcm is too large to store as an int * @since 1.1 */ public static int lcm(int a, int b) { return Math.abs(mulAndCheck(a / gcd(a, b), b)); }
/** * Returns the least common multiple between two integer values. * * @param a the first integer value. * @param b the second integer value. * @return the least common multiple between a and b. * @throws ArithmeticException if the lcm is too large to store as an int * @since 1.1 */ public static int lcm(int a, int b) { return Math.abs(mulAndCheck(a / gcd(a, b), b)); }
int d = MathUtils.gcd(num, den); if (d > 1) { num /= d;
int gcd = MathUtils.gcd(numerator, denominator); 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 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>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 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 = 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)); }
return 0; int lcm = Math.abs(mulAndCheck(a / gcd(a, b), b)); if (lcm == Integer.MIN_VALUE) { throw MathRuntimeException.createArithmeticException(
return 0; long lcm = Math.abs(mulAndCheck(a / gcd(a, b), b)); if (lcm == Long.MIN_VALUE){ throw MathRuntimeException.createArithmeticException(
/** * <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)); }
final int d = MathUtils.gcd(num, den); if (d > 1) { num /= d;
final int d = MathUtils.gcd(num, den); if (d > 1) { num /= d;
return 0; int lcm = FastMath.abs(mulAndCheck(a / gcd(a, b), b)); if (lcm == Integer.MIN_VALUE) { throw MathRuntimeException.createArithmeticException(
return 0; long lcm = FastMath.abs(mulAndCheck(a / gcd(a, b), b)); if (lcm == Long.MIN_VALUE){ throw MathRuntimeException.createArithmeticException(
int gcd = MathUtils.gcd(numerator, denominator); numerator /= gcd; denominator /= gcd;
int gcd = MathUtils.gcd(numerator, denominator); numerator /= gcd; denominator /= gcd;