@Test public void testRoundFraction() { final int[] numerators = { 0, 1, 2, 3, 9, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19}; final int[] denominators = { 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4}; final int[] results = { 0, 0, 1, 1, 3, 3, 4, 4, 3, 3, 4, 4, 4, 4, 4, 5}; for (int i=10; i<numerators.length; i++) { for (int s=0; s<4; s++) { int numerator = numerators [i]; int denominator = denominators[i]; int expected = results [i]; if ((s & 1) != 0) numerator = -numerator; if ((s & 2) != 0) denominator = -denominator; if (numerator * denominator < 0) expected = -expected; final String label = "even(" + numerator + '/' + denominator + ')'; assertEquals(label, expected, Fraction.round(numerator, denominator)); } } }
public static long round(final long numerator, final long denominator) { long n = numerator / denominator; long r = numerator % denominator; if (r != 0) { r = Math.abs(r << 1); final long d = Math.abs(denominator); if (r > d || (r == d && (n & 1) != 0)) { if ((numerator ^ denominator) >= 0) { n++; } else { n--; } } } return n; }
Fraction extends Number implements Comparable<Fraction>, Cloneable { public static long round(final long numerator, final long denominator) { long n = numerator / denominator; long r = numerator % denominator; if (r != 0) { r = Math.abs(r << 1); final long d = Math.abs(denominator); if (r > d || (r == d && (n & 1) != 0)) { if ((numerator ^ denominator) >= 0) { n++; } else { n--; } } } return n; } }
Fraction extends Number implements Comparable<Fraction>, Cloneable { public static long round(final long numerator, final long denominator) { long n = numerator / denominator; long r = numerator % denominator; if (r != 0) { r = Math.abs(r << 1); final long d = Math.abs(denominator); if (r > d || (r == d && (n & 1) != 0)) { if ((numerator ^ denominator) >= 0) { n++; } else { n--; } } } return n; } Fraction();  Fraction(final Fraction other);  Fraction(final int numerator);  Fraction(final int numerator, final int denominator); }
Fraction extends Number implements Comparable<Fraction>, Cloneable { public static long round(final long numerator, final long denominator) { long n = numerator / denominator; long r = numerator % denominator; if (r != 0) { r = Math.abs(r << 1); final long d = Math.abs(denominator); if (r > d || (r == d && (n & 1) != 0)) { if ((numerator ^ denominator) >= 0) { n++; } else { n--; } } } return n; } Fraction();  Fraction(final Fraction other);  Fraction(final int numerator);  Fraction(final int numerator, final int denominator); void set(final int numerator, final int denominator); void add(final Fraction other); void subtract(final Fraction other); void multiply(final Fraction other); void divide(final Fraction other); int numerator(); int denominator(); @Override double doubleValue(); @Override float floatValue(); @Override long longValue(); @Override int intValue(); static long round(final long numerator, final long denominator); static int round(final int numerator, final int denominator); static int floor(final int numerator, final int denominator); static int ceil(final int numerator, final int denominator); int compareTo(final Fraction other); @Override boolean equals(final Object other); @Override Fraction clone(); @Override String toString(); }
Fraction extends Number implements Comparable<Fraction>, Cloneable { public static long round(final long numerator, final long denominator) { long n = numerator / denominator; long r = numerator % denominator; if (r != 0) { r = Math.abs(r << 1); final long d = Math.abs(denominator); if (r > d || (r == d && (n & 1) != 0)) { if ((numerator ^ denominator) >= 0) { n++; } else { n--; } } } return n; } Fraction();  Fraction(final Fraction other);  Fraction(final int numerator);  Fraction(final int numerator, final int denominator); void set(final int numerator, final int denominator); void add(final Fraction other); void subtract(final Fraction other); void multiply(final Fraction other); void divide(final Fraction other); int numerator(); int denominator(); @Override double doubleValue(); @Override float floatValue(); @Override long longValue(); @Override int intValue(); static long round(final long numerator, final long denominator); static int round(final int numerator, final int denominator); static int floor(final int numerator, final int denominator); static int ceil(final int numerator, final int denominator); int compareTo(final Fraction other); @Override boolean equals(final Object other); @Override Fraction clone(); @Override String toString();  }