/** * <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 properNumerator = getProperNumerator(); if (properNumerator == 0) { toProperString = Integer.toString(getProperWhole()); } else { toProperString = new StrBuilder(32) .append(getProperWhole()).append(' ') .append(properNumerator).append('/') .append(getDenominator()).toString(); .append(getNumerator()).append('/') .append(getDenominator()).toString();
return isAdd ? fraction : fraction.negate(); int d1 = greatestCommonDivisor(denominator, fraction.denominator); if (d1==1) { int uvp = mulAndCheck(numerator, fraction.denominator); int upv = mulAndCheck(fraction.numerator, denominator); return new Fraction (isAdd ? addAndCheck(uvp, upv) : subAndCheck(uvp, upv), mulPosAndCheck(denominator, fraction.denominator)); int d2 = (tmodd1==0)?d1:greatestCommonDivisor(tmodd1, d1); ("overflow: numerator too large after multiply"); return new Fraction (w.intValue(), mulPosAndCheck(denominator/d1, fraction.denominator/d2));
/** * <p>Gets a hashCode for the fraction.</p> * * @return a hash code value for this object */ public int hashCode() { if (hashCode == 0) { // hashcode update should be atomic. hashCode = 37 * (37 * 17 + getNumerator()) + getDenominator(); } return hashCode; }
/** * <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)); }
public static String convertFramerate(double vrate) { Fraction f1 = Fraction.getFraction((int) (vrate * 1001), 1001); Fraction f2 = Fraction.getFraction((int) (vrate * 1000), 1000); double d1 = Math.abs(f1.doubleValue() - vrate); double d2 = Math.abs(f2.doubleValue() - vrate); if (d1 < d2) { return f1.getNumerator() + "/" + f1.getDenominator(); } else { return f2.getNumerator() + "/" + f2.getDenominator(); } }
/** * <p>Gets a fraction that is the negative (-fraction) of this one.</p> * * <p>The returned fraction is not reduced.</p> * * @return a new fraction instance with the opposite signed numerator */ public Fraction negate() { // the positive range is one smaller than the negative range of an int. if (numerator==Integer.MIN_VALUE) { throw new ArithmeticException("overflow: too large to negate"); } return new Fraction(-numerator, denominator); }
int gcd = greatestCommonDivisor(numerator, denominator); numerator /= gcd; denominator /= gcd; return new Fraction(numerator, denominator);
public static void adjustMinMaxFrameRate(AdaptationSetType adaptationSetType) { List<RepresentationType> representations = adaptationSetType.getRepresentation(); Fraction min = null, max = null; for (RepresentationType representationType : representations) { String frameRate = representationType.getFrameRate(); if (frameRate != null) { Fraction f = Fraction.getFraction(frameRate); min = min == null || f.compareTo(min) < 0 ? f : min; max = max == null || f.compareTo(max) > 0 ? f : max; } } if (max != null && !min.equals(max)) { // min/max doesn't make sense when both values are the same adaptationSetType.setMinFrameRate(min.toString()); adaptationSetType.setMaxFrameRate(max.toString()); } }
return getFraction(Double.parseDouble(str)); int numer = Integer.parseInt(str.substring(0, pos)); int denom = Integer.parseInt(str.substring(pos + 1)); return getFraction(whole, numer, denom); if (pos < 0) { return getFraction(Integer.parseInt(str), 1); } else { int numer = Integer.parseInt(str.substring(0, pos)); int denom = Integer.parseInt(str.substring(pos + 1)); return getFraction(numer, denom);
/** * <p>Adds the value of this fraction to another, returning the result in reduced form. * The algorithm follows Knuth, 4.5.1.</p> * * @param fraction the fraction to add, 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 add(Fraction fraction) { return addSub(fraction, true /* add */); }
public boolean failuresRatioExceeds(double threshold) { if (hasFailures()) { Fraction failuresRatio = getFraction(failures.intValue(), failures.intValue() + successes.intValue()); return failuresRatio.compareTo(getFraction(threshold)) > 0; } return false; }
throw new ArithmeticException("Unable to convert double to fraction"); return getReducedFraction((numer0 + wholeNumber * denom0) * sign, denom0);
Fraction a = // this is however you're making a fraction object... Fraction b = // do EXACT same thing here that you did for a // And then, this will illustrate what is wrong with your program... if(a.equals(b)) { System.out.println("This won't print"); } else { System.out.println("This will print because your method just checks for reference"); }
/** * <p>Compares this fraction to another object to test if they are equal.</p>. * * <p>To be equal, both values must be equal. Thus 2/4 is not equal to 1/2.</p> * * @param obj the reference object with which to compare * @return <code>true</code> if this object is equal */ public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof Fraction == false) { return false; } Fraction other = (Fraction) obj; return (getNumerator() == other.getNumerator() && getDenominator() == other.getDenominator()); }
/** * <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>Gets a fraction that is the inverse (1/fraction) of this one.</p> * * <p>The returned fraction is not reduced.</p> * * @return a new fraction instance with the numerator and denominator * inverted. * @throws ArithmeticException if the fraction represents zero. */ public Fraction invert() { if (numerator == 0) { throw new ArithmeticException("Unable to invert zero."); } if (numerator==Integer.MIN_VALUE) { throw new ArithmeticException("overflow: can't negate numerator"); } if (numerator<0) { return new Fraction(-denominator, -numerator); } else { return new Fraction(denominator, numerator); } }
int gcd = greatestCommonDivisor(numerator, denominator); numerator /= gcd; denominator /= gcd; return new Fraction(numerator, denominator);
return getFraction(Double.parseDouble(str)); int numer = Integer.parseInt(str.substring(0, pos)); int denom = Integer.parseInt(str.substring(pos + 1)); return getFraction(whole, numer, denom); if (pos < 0) { return getFraction(Integer.parseInt(str), 1); } else { int numer = Integer.parseInt(str.substring(0, pos)); int denom = Integer.parseInt(str.substring(pos + 1)); return getFraction(numer, denom);
/** * <p>Subtracts the value of another fraction from the value of this one, * returning the result in reduced form.</p> * * @param fraction the fraction to subtract, 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 * cannot be represented in an <code>int</code>. */ public Fraction subtract(Fraction fraction) { return addSub(fraction, false /* subtract */); }