Beispiel #1
0
  public void testConstructor() {
    assertFraction(0, 1, new Fraction(0, 1));
    assertFraction(0, 1, new Fraction(0, 2));
    assertFraction(0, 1, new Fraction(0, -1));
    assertFraction(1, 2, new Fraction(1, 2));
    assertFraction(1, 2, new Fraction(2, 4));
    assertFraction(-1, 2, new Fraction(-1, 2));
    assertFraction(-1, 2, new Fraction(1, -2));
    assertFraction(-1, 2, new Fraction(-2, 4));
    assertFraction(-1, 2, new Fraction(2, -4));

    // overflow
    try {
      new Fraction(Integer.MIN_VALUE, -1);
      fail();
    } catch (ArithmeticException ex) {
      // success
    }
    try {
      new Fraction(1, Integer.MIN_VALUE);
      fail();
    } catch (ArithmeticException ex) {
      // success
    }
    try {
      assertFraction(0, 1, new Fraction(0.00000000000001));
      assertFraction(2, 5, new Fraction(0.40000000000001));
      assertFraction(15, 1, new Fraction(15.0000000000001));

    } catch (ConvergenceException ex) {
      fail(ex.getMessage());
    }
  }
Beispiel #2
0
 public void testConstructorDouble() {
   try {
     assertFraction(1, 2, new Fraction(0.5));
     assertFraction(1, 3, new Fraction(1.0 / 3.0));
     assertFraction(17, 100, new Fraction(17.0 / 100.0));
     assertFraction(317, 100, new Fraction(317.0 / 100.0));
     assertFraction(-1, 2, new Fraction(-0.5));
     assertFraction(-1, 3, new Fraction(-1.0 / 3.0));
     assertFraction(-17, 100, new Fraction(17.0 / -100.0));
     assertFraction(-317, 100, new Fraction(-317.0 / 100.0));
   } catch (ConvergenceException ex) {
     fail(ex.getMessage());
   }
 }