/** * <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)); }
if (d1==1) { int uvp = ArithmeticUtils.mulAndCheck(numerator, fraction.denominator); int upv = ArithmeticUtils.mulAndCheck(fraction.numerator, denominator); return new Fraction (isAdd ? ArithmeticUtils.addAndCheck(uvp, upv) : ArithmeticUtils.subAndCheck(uvp, upv), ArithmeticUtils.mulAndCheck(denominator, fraction.denominator)); ArithmeticUtils.mulAndCheck(denominator/d1, fraction.denominator/d2));
return 0; long lcm = FastMath.abs(ArithmeticUtils.mulAndCheck(a / gcd(a, b), b)); if (lcm == Long.MIN_VALUE){ throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_64_BITS,
return 0; int lcm = FastMath.abs(ArithmeticUtils.mulAndCheck(a / gcd(a, b), b)); if (lcm == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_32_BITS,
if (a > b) { ret = mulAndCheck(b, a); } else { if (a < 0) {
while (true) { if ((exp & 0x1) != 0) { result = mulAndCheck(result, k2p); k2p = mulAndCheck(k2p, k2p);
while (true) { if ((exp & 0x1) != 0) { result = mulAndCheck(result, k2p); k2p = mulAndCheck(k2p, k2p);
for (int j = 1; j <= k; j++) { final long d = ArithmeticUtils.gcd(i, j); result = ArithmeticUtils.mulAndCheck(result / (j / d), i / d); i++;
return 0; long lcm = Math.abs(ArithmeticUtils.mulAndCheck(a / gcd(a, b), b)); if (lcm == Long.MIN_VALUE){ throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_64_BITS,
return 0; int lcm = Math.abs(ArithmeticUtils.mulAndCheck(a / gcd(a, b), b)); if (lcm == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_32_BITS,
if (d1==1) { int uvp = ArithmeticUtils.mulAndCheck(numerator, fraction.denominator); int upv = ArithmeticUtils.mulAndCheck(fraction.numerator, denominator); return new Fraction (isAdd ? ArithmeticUtils.addAndCheck(uvp, upv) : ArithmeticUtils.subAndCheck(uvp, upv), ArithmeticUtils.mulAndCheck(denominator, fraction.denominator)); ArithmeticUtils.mulAndCheck(denominator/d1, fraction.denominator/d2));
/** * <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)); }
/** * <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)); }
return 0; long lcm = FastMath.abs(ArithmeticUtils.mulAndCheck(a / gcd(a, b), b)); if (lcm == Long.MIN_VALUE){ throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_64_BITS,
return 0; int lcm = FastMath.abs(ArithmeticUtils.mulAndCheck(a / gcd(a, b), b)); if (lcm == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_32_BITS,
if (a > b) { ret = mulAndCheck(b, a); } else { if (a < 0) {
while (true) { if ((exp & 0x1) != 0) { result = mulAndCheck(result, k2p); k2p = mulAndCheck(k2p, k2p);
while (true) { if ((exp & 0x1) != 0) { result = mulAndCheck(result, k2p); k2p = mulAndCheck(k2p, k2p);
while (true) { if ((exp & 0x1) != 0) { result = mulAndCheck(result, k2p); k2p = mulAndCheck(k2p, k2p);
while (true) { if ((exp & 0x1) != 0) { result = mulAndCheck(result, k2p); k2p = mulAndCheck(k2p, k2p);