Exemple #1
0
  public void testReciprocal() {
    Fraction f = null;

    f = new Fraction(50, 75);
    f = f.reciprocal();
    assertEquals(3, f.getNumerator());
    assertEquals(2, f.getDenominator());

    f = new Fraction(4, 3);
    f = f.reciprocal();
    assertEquals(3, f.getNumerator());
    assertEquals(4, f.getDenominator());

    f = new Fraction(-15, 47);
    f = f.reciprocal();
    assertEquals(-47, f.getNumerator());
    assertEquals(15, f.getDenominator());

    f = new Fraction(0, 3);
    try {
      f = f.reciprocal();
      fail("expecting ArithmeticException");
    } catch (ArithmeticException ex) {
    }

    // large values
    f = new Fraction(Integer.MAX_VALUE, 1);
    f = f.reciprocal();
    assertEquals(1, f.getNumerator());
    assertEquals(Integer.MAX_VALUE, f.getDenominator());
  }
Exemple #2
0
  public void testDivide() {
    Fraction a = new Fraction(1, 2);
    Fraction b = new Fraction(2, 3);

    assertFraction(1, 1, a.divide(a));
    assertFraction(3, 4, a.divide(b));
    assertFraction(4, 3, b.divide(a));
    assertFraction(1, 1, b.divide(b));

    Fraction f1 = new Fraction(3, 5);
    Fraction f2 = Fraction.ZERO;
    try {
      f1.divide(f2);
      fail("expecting ArithmeticException");
    } catch (ArithmeticException ex) {
    }

    f1 = new Fraction(0, 5);
    f2 = new Fraction(2, 7);
    Fraction f = f1.divide(f2);
    assertSame(Fraction.ZERO, f);

    f1 = new Fraction(2, 7);
    f2 = Fraction.ONE;
    f = f1.divide(f2);
    assertEquals(2, f.getNumerator());
    assertEquals(7, f.getDenominator());

    f1 = new Fraction(1, Integer.MAX_VALUE);
    f = f1.divide(f1);
    assertEquals(1, f.getNumerator());
    assertEquals(1, f.getDenominator());

    f1 = new Fraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
    f2 = new Fraction(1, Integer.MAX_VALUE);
    f = f1.divide(f2);
    assertEquals(Integer.MIN_VALUE, f.getNumerator());
    assertEquals(1, f.getDenominator());

    try {
      f.divide(null);
      fail("IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
    }

    try {
      f1 = new Fraction(1, Integer.MAX_VALUE);
      f = f1.divide(f1.reciprocal()); // should overflow
      fail("expecting ArithmeticException");
    } catch (ArithmeticException ex) {
    }
    try {
      f1 = new Fraction(1, -Integer.MAX_VALUE);
      f = f1.divide(f1.reciprocal()); // should overflow
      fail("expecting ArithmeticException");
    } catch (ArithmeticException ex) {
    }
  }
 /**
  * Divide the value of this fraction by another.
  *
  * @param fraction the fraction to divide 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 fraction to divide by is zero
  * @throws ArithmeticException if the resulting numerator or denominator exceeds <code>
  *     Integer.MAX_VALUE</code>
  */
 public Fraction divide(Fraction fraction) {
   if (fraction == null) {
     throw MathRuntimeException.createIllegalArgumentException("null fraction");
   }
   if (fraction.numerator == 0) {
     throw MathRuntimeException.createArithmeticException(
         "the fraction to divide by must not be zero: {0}/{1}",
         fraction.numerator, fraction.denominator);
   }
   return multiply(fraction.reciprocal());
 }