Fraction extends Number implements FieldElement<Fraction>, Comparable<Fraction>, Serializable { public int compareTo(Fraction object) { long nOd = ((long) numerator) * object.denominator; long dOn = ((long) denominator) * object.numerator; return (nOd < dOn) ? -1 : ((nOd > dOn) ? +1 : 0); } Fraction(double value);  Fraction(double value, double epsilon, int maxIterations);  Fraction(double value, int maxDenominator); private  Fraction(double value, double epsilon, int maxDenominator, int maxIterations);  Fraction(int num);  Fraction(int num, int den); Fraction abs(); int compareTo(Fraction object); @Override double doubleValue(); @Override boolean equals(Object other); @Override float floatValue(); int getDenominator(); int getNumerator(); @Override int hashCode(); @Override int intValue(); @Override long longValue(); Fraction negate(); Fraction reciprocal(); Fraction add(Fraction fraction); Fraction add(final int i); Fraction subtract(Fraction fraction); Fraction subtract(final int i); Fraction multiply(Fraction fraction); Fraction multiply(final int i); Fraction divide(Fraction fraction); Fraction divide(final int i); double percentageValue(); static Fraction getReducedFraction(int numerator, int denominator); @Override String toString(); FractionField getField(); static final Fraction TWO; static final Fraction ONE; static final Fraction ZERO; static final Fraction FOUR_FIFTHS; static final Fraction ONE_FIFTH; static final Fraction ONE_HALF; static final Fraction ONE_QUARTER; static final Fraction ONE_THIRD; static final Fraction THREE_FIFTHS; static final Fraction THREE_QUARTERS; static final Fraction TWO_FIFTHS; static final Fraction TWO_QUARTERS; static final Fraction TWO_THIRDS; static final Fraction MINUS_ONE; }
@Test public void testCompareTo() { Fraction first = new Fraction(1, 2); Fraction second = new Fraction(1, 3); Fraction third = new Fraction(1, 2); Assert.assertEquals(0, first.compareTo(first)); Assert.assertEquals(0, first.compareTo(third)); Assert.assertEquals(1, first.compareTo(second)); Assert.assertEquals(-1, second.compareTo(first)); Fraction pi1 = new Fraction(1068966896, 340262731); Fraction pi2 = new Fraction( 411557987, 131002976); Assert.assertEquals(-1, pi1.compareTo(pi2)); Assert.assertEquals( 1, pi2.compareTo(pi1)); Assert.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20); }