/** * <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> * Returns a <code>BigFraction</code> whose value is * <tt>(this<sup>exponent</sup>)</tt>, returning the result in reduced form. * </p> * * @param exponent * exponent to which this <code>BigFraction</code> is to be raised. * @return <tt>this<sup>exponent</sup></tt> as a <code>BigFraction</code>. */ public BigFraction pow(final long exponent) { if (exponent == 0) { return ONE; } if (numerator.signum() == 0) { return this; } if (exponent < 0) { return new BigFraction(ArithmeticUtils.pow(denominator, -exponent), ArithmeticUtils.pow(numerator, -exponent)); } return new BigFraction(ArithmeticUtils.pow(numerator, exponent), ArithmeticUtils.pow(denominator, exponent)); }
int d1 = ArithmeticUtils.gcd(denominator, fraction.denominator); 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)); int d2 = (tmodd1==0)?d1:ArithmeticUtils.gcd(tmodd1, d1); ArithmeticUtils.mulAndCheck(denominator/d1, fraction.denominator/d2));
final int d = ArithmeticUtils.gcd(num, den); if (d > 1) { num /= d;
final int halfN = n / 2; if (!ArithmeticUtils.isPowerOfTwo(n)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO,
if (a > b) { ret = mulAndCheck(b, a); } else { if (a < 0) {
/** * Add two long integers, checking for overflow. * * @param a an addend * @param b an addend * @return the sum {@code a+b} * @throws MathArithmeticException if the result can not be represented as an long * @since 1.2 */ public static long addAndCheck(long a, long b) throws MathArithmeticException { return addAndCheck(a, b, LocalizedFormats.OVERFLOW_IN_ADDITION); }
long len = file.getFileStatus().getLen(); long blockSize = ArithmeticUtils.lcm(file.getFileStatus().getBlockSize(), file.getBlockSize(targetFs)); long maxSplitSize = workUnit.getPropAsLong(MAX_SPLIT_SIZE_KEY, DEFAULT_MAX_SPLIT_SIZE);
return gcdPositive(a, b);
private synchronized void configureSinks() { sinkConfigs = config.getInstanceConfigs(SINK_KEY); long confPeriodMillis = 0; for (Entry<String, MetricsConfig> entry : sinkConfigs.entrySet()) { MetricsConfig conf = entry.getValue(); int sinkPeriod = conf.getInt(PERIOD_KEY, PERIOD_DEFAULT); // Support configuring periodMillis for testing. long sinkPeriodMillis = conf.getLong(PERIOD_MILLIS_KEY, sinkPeriod * 1000); confPeriodMillis = confPeriodMillis == 0 ? sinkPeriodMillis : ArithmeticUtils.gcd(confPeriodMillis, sinkPeriodMillis); String clsName = conf.getClassName(""); if (clsName == null) continue; // sink can be registered later on String sinkName = entry.getKey(); try { MetricsSinkAdapter sa = newSink(sinkName, conf.getString(DESC_KEY, sinkName), conf); sa.start(); sinks.put(sinkName, sa); } catch (Exception e) { LOG.warn("Error creating sink '"+ sinkName +"'", e); } } long periodSec = config.getInt(PERIOD_KEY, PERIOD_DEFAULT); period = confPeriodMillis > 0 ? confPeriodMillis : config.getLong(PERIOD_MILLIS_KEY, periodSec * 1000); }
final int halfN = n / 2; if (!ArithmeticUtils.isPowerOfTwo(n)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO,
while (true) { if ((exp & 0x1) != 0) { result = mulAndCheck(result, k2p); k2p = mulAndCheck(k2p, k2p);
/** * Subtract two long integers, checking for overflow. * * @param a Value. * @param b Value. * @return the difference {@code a - b}. * @throws MathArithmeticException if the result can not be represented as a * {@code long}. * @since 1.2 */ public static long subAndCheck(long a, long b) throws MathArithmeticException { long ret; if (b == Long.MIN_VALUE) { if (a < 0) { ret = a - b; } else { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, -b); } } else { // use additive inverse ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION); } return ret; }
/** * Calculates the width/ height of "repeated" sprites. */ static int calculateLeastCommonMultiple( Map<SpriteReferenceOccurrence, BufferedImage> images, SpriteImageLayout layout) { int leastCommonMultiple = 1; for (final Map.Entry<SpriteReferenceOccurrence, BufferedImage> entry : images .entrySet()) { final BufferedImage image = entry.getValue(); final SpriteReferenceOccurrence spriteReferenceOccurrence = entry.getKey(); if (image != null && SpriteAlignment.REPEAT .equals(spriteReferenceOccurrence.spriteReferenceDirective.spriteLayoutProperties.alignment)) { if (SpriteImageLayout.VERTICAL.equals(layout)) { leastCommonMultiple = ArithmeticUtils.lcm(leastCommonMultiple, spriteReferenceOccurrence.getRequiredWidth(image, layout)); } else { leastCommonMultiple = ArithmeticUtils.lcm(leastCommonMultiple, spriteReferenceOccurrence.getRequiredHeight(image, layout)); } } } return leastCommonMultiple; }
return gcdPositive(a, b);
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,
int gcd = ArithmeticUtils.gcd(numerator, denominator); numerator /= gcd; denominator /= gcd;
int d1 = ArithmeticUtils.gcd(denominator, fraction.denominator); 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)); int d2 = (tmodd1==0)?d1:ArithmeticUtils.gcd(tmodd1, d1); ArithmeticUtils.mulAndCheck(denominator/d1, fraction.denominator/d2));
/** * <p> * Returns a <code>BigFraction</code> whose value is * <tt>(this<sup>exponent</sup>)</tt>, returning the result in reduced form. * </p> * * @param exponent * exponent to which this <code>BigFraction</code> is to be raised. * @return <tt>this<sup>exponent</sup></tt> as a <code>BigFraction</code>. */ public BigFraction pow(final BigInteger exponent) { if (exponent.signum() == 0) { return ONE; } if (numerator.signum() == 0) { return this; } if (exponent.signum() == -1) { final BigInteger eNeg = exponent.negate(); return new BigFraction(ArithmeticUtils.pow(denominator, eNeg), ArithmeticUtils.pow(numerator, eNeg)); } return new BigFraction(ArithmeticUtils.pow(numerator, exponent), ArithmeticUtils.pow(denominator, exponent)); }