@Test public void testSimplify() { Fraction fraction = new Fraction(4, 7); assertEquals(4, fraction.numerator()); assertEquals(7, fraction.denominator()); fraction.set(4, 8); assertEquals(1, fraction.numerator()); assertEquals(2, fraction.denominator()); fraction.set(17*21, 31*21); assertEquals(17, fraction.numerator()); assertEquals(31, fraction.denominator()); }
private void simplify() { if (numerator == 0) { denominator = XMath.sgn(denominator); return; } if (denominator == 0) { numerator = XMath.sgn(numerator); return; } if (denominator % numerator == 0) { denominator /= numerator; if (denominator < 0) { denominator = -denominator; numerator = -1; } else { numerator = 1; } return; } int num = Math.abs(numerator); int den = Math.abs(denominator); num %= den; if (num == 0) { numerator /= denominator; denominator = 1; return; } int pgcd = 1; int remainder = num; do { num = den; den = remainder; pgcd = remainder; remainder = num % den; } while (remainder != 0); numerator /= pgcd; denominator /=pgcd; if (denominator < 0) { numerator = -numerator; denominator = -denominator; } }
Fraction extends Number implements Comparable<Fraction>, Cloneable { private void simplify() { if (numerator == 0) { denominator = XMath.sgn(denominator); return; } if (denominator == 0) { numerator = XMath.sgn(numerator); return; } if (denominator % numerator == 0) { denominator /= numerator; if (denominator < 0) { denominator = -denominator; numerator = -1; } else { numerator = 1; } return; } int num = Math.abs(numerator); int den = Math.abs(denominator); num %= den; if (num == 0) { numerator /= denominator; denominator = 1; return; } int pgcd = 1; int remainder = num; do { num = den; den = remainder; pgcd = remainder; remainder = num % den; } while (remainder != 0); numerator /= pgcd; denominator /=pgcd; if (denominator < 0) { numerator = -numerator; denominator = -denominator; } } }
Fraction extends Number implements Comparable<Fraction>, Cloneable { private void simplify() { if (numerator == 0) { denominator = XMath.sgn(denominator); return; } if (denominator == 0) { numerator = XMath.sgn(numerator); return; } if (denominator % numerator == 0) { denominator /= numerator; if (denominator < 0) { denominator = -denominator; numerator = -1; } else { numerator = 1; } return; } int num = Math.abs(numerator); int den = Math.abs(denominator); num %= den; if (num == 0) { numerator /= denominator; denominator = 1; return; } int pgcd = 1; int remainder = num; do { num = den; den = remainder; pgcd = remainder; remainder = num % den; } while (remainder != 0); numerator /= pgcd; denominator /=pgcd; if (denominator < 0) { numerator = -numerator; denominator = -denominator; } } Fraction();  Fraction(final Fraction other);  Fraction(final int numerator);  Fraction(final int numerator, final int denominator); }
Fraction extends Number implements Comparable<Fraction>, Cloneable { private void simplify() { if (numerator == 0) { denominator = XMath.sgn(denominator); return; } if (denominator == 0) { numerator = XMath.sgn(numerator); return; } if (denominator % numerator == 0) { denominator /= numerator; if (denominator < 0) { denominator = -denominator; numerator = -1; } else { numerator = 1; } return; } int num = Math.abs(numerator); int den = Math.abs(denominator); num %= den; if (num == 0) { numerator /= denominator; denominator = 1; return; } int pgcd = 1; int remainder = num; do { num = den; den = remainder; pgcd = remainder; remainder = num % den; } while (remainder != 0); numerator /= pgcd; denominator /=pgcd; if (denominator < 0) { numerator = -numerator; denominator = -denominator; } } 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 { private void simplify() { if (numerator == 0) { denominator = XMath.sgn(denominator); return; } if (denominator == 0) { numerator = XMath.sgn(numerator); return; } if (denominator % numerator == 0) { denominator /= numerator; if (denominator < 0) { denominator = -denominator; numerator = -1; } else { numerator = 1; } return; } int num = Math.abs(numerator); int den = Math.abs(denominator); num %= den; if (num == 0) { numerator /= denominator; denominator = 1; return; } int pgcd = 1; int remainder = num; do { num = den; den = remainder; pgcd = remainder; remainder = num % den; } while (remainder != 0); numerator /= pgcd; denominator /=pgcd; if (denominator < 0) { numerator = -numerator; denominator = -denominator; } } 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();  }