Exemple #1
0
  public void testFloatValue() {
    Fraction first = new Fraction(1, 2);
    Fraction second = new Fraction(1, 3);

    assertEquals(0.5f, first.floatValue(), 0.0f);
    assertEquals((float) (1.0 / 3.0), second.floatValue(), 0.0f);
  }
Exemple #2
0
  public void testDoubleValue() {
    Fraction first = new Fraction(1, 2);
    Fraction second = new Fraction(1, 3);

    assertEquals(0.5, first.doubleValue(), 0.0);
    assertEquals(1.0 / 3.0, second.doubleValue(), 0.0);
  }
Exemple #3
0
  public void testLongValue() {
    Fraction first = new Fraction(1, 2);
    Fraction second = new Fraction(3, 2);

    assertEquals(0L, first.longValue());
    assertEquals(1L, second.longValue());
  }
Exemple #4
0
  public void testIntValue() {
    Fraction first = new Fraction(1, 2);
    Fraction second = new Fraction(3, 2);

    assertEquals(0, first.intValue());
    assertEquals(1, second.intValue());
  }
Exemple #5
0
 public void registerValue(String column, String row, double value) {
   Fraction fraction = toFraction(value);
   if (toReverse.contains(row)) {
     fraction = fraction.negate();
   }
   columns.add(column);
   writeToFile(row, column, fraction);
 }
Exemple #6
0
 public void registerBound(MPSBoundType type, String variable, double value) {
   Fraction bound = toFraction(value);
   Fraction cell = Fraction.ONE;
   switch (type) {
     case LO:
       bound = bound.negate();
       cell = cell.negate();
       // fall-through.
     case UP:
       addBound(variable, cell, bound);
       break;
     default:
       MPSReader.LOG.info("Ignoring bound type: " + type);
   }
 }
 /**
  * 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());
 }
Exemple #8
0
  public void testMultiply() {
    Fraction a = new Fraction(1, 2);
    Fraction b = new Fraction(2, 3);

    assertFraction(1, 4, a.multiply(a));
    assertFraction(1, 3, a.multiply(b));
    assertFraction(1, 3, b.multiply(a));
    assertFraction(4, 9, b.multiply(b));

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

    try {
      f.multiply(null);
      fail("expecting IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
    }
  }
Exemple #9
0
  public void testAbs() {
    Fraction a = new Fraction(10, 21);
    Fraction b = new Fraction(-10, 21);
    Fraction c = new Fraction(10, -21);

    assertFraction(10, 21, a.abs());
    assertFraction(10, 21, b.abs());
    assertFraction(10, 21, c.abs());
  }
Exemple #10
0
 public void testGetReducedFraction() {
   Fraction threeFourths = new Fraction(3, 4);
   assertTrue(threeFourths.equals(Fraction.getReducedFraction(6, 8)));
   assertTrue(Fraction.ZERO.equals(Fraction.getReducedFraction(0, -1)));
   try {
     Fraction.getReducedFraction(1, 0);
     fail("expecting ArithmeticException");
   } catch (ArithmeticException ex) {
     // expected
   }
   assertEquals(Fraction.getReducedFraction(2, Integer.MIN_VALUE).getNumerator(), -1);
   assertEquals(Fraction.getReducedFraction(1, -1).getNumerator(), -1);
 }
Exemple #11
0
  /**
   * Implement add and subtract using algorithm described in Knuth 4.5.1.
   *
   * @param fraction the fraction to subtract, must not be <code>null</code>
   * @param isAdd true to add, false to subtract
   * @return a <code>Fraction</code> instance with the resulting values
   * @throws IllegalArgumentException if the fraction is <code>null</code>
   * @throws ArithmeticException if the resulting numerator or denominator cannot be represented in
   *     an <code>int</code>.
   */
  private Fraction addSub(Fraction fraction, boolean isAdd) {
    if (fraction == null) {
      throw MathRuntimeException.createIllegalArgumentException("null fraction");
    }
    // zero is identity for addition.
    if (numerator == 0) {
      return isAdd ? fraction : fraction.negate();
    }
    if (fraction.numerator == 0) {
      return this;
    }
    // if denominators are randomly distributed, d1 will be 1 about 61%
    // of the time.
    int d1 = MathUtils.gcd(denominator, fraction.denominator);
    if (d1 == 1) {
      // result is ( (u*v' +/- u'v) / u'v')
      int uvp = MathUtils.mulAndCheck(numerator, fraction.denominator);
      int upv = MathUtils.mulAndCheck(fraction.numerator, denominator);
      return new Fraction(
          isAdd ? MathUtils.addAndCheck(uvp, upv) : MathUtils.subAndCheck(uvp, upv),
          MathUtils.mulAndCheck(denominator, fraction.denominator));
    }
    // the quantity 't' requires 65 bits of precision; see knuth 4.5.1
    // exercise 7.  we're going to use a BigInteger.
    // t = u(v'/d1) +/- v(u'/d1)
    BigInteger uvp =
        BigInteger.valueOf(numerator).multiply(BigInteger.valueOf(fraction.denominator / d1));
    BigInteger upv =
        BigInteger.valueOf(fraction.numerator).multiply(BigInteger.valueOf(denominator / d1));
    BigInteger t = isAdd ? uvp.add(upv) : uvp.subtract(upv);
    // but d2 doesn't need extra precision because
    // d2 = gcd(t,d1) = gcd(t mod d1, d1)
    int tmodd1 = t.mod(BigInteger.valueOf(d1)).intValue();
    int d2 = (tmodd1 == 0) ? d1 : MathUtils.gcd(tmodd1, d1);

    // result is (t/d2) / (u'/d1)(v'/d2)
    BigInteger w = t.divide(BigInteger.valueOf(d2));
    if (w.bitLength() > 31) {
      throw MathRuntimeException.createArithmeticException(
          "overflow, numerator too large after multiply: {0}", w);
    }
    return new Fraction(
        w.intValue(), MathUtils.mulAndCheck(denominator / d1, fraction.denominator / d2));
  }
Exemple #12
0
 private void assertFraction(int expectedNumerator, int expectedDenominator, Fraction actual) {
   assertEquals(expectedNumerator, actual.getNumerator());
   assertEquals(expectedDenominator, actual.getDenominator());
 }
 /** {@inheritDoc} */
 @Override
 public void visit(int row, int column, Fraction value) {
   data[row][column] = value.doubleValue();
 }
Exemple #14
0
 public void testEqualsAndHashCode() {
   Fraction zero = new Fraction(0, 1);
   Fraction nullFraction = null;
   assertTrue(zero.equals(zero));
   assertFalse(zero.equals(nullFraction));
   assertFalse(zero.equals(Double.valueOf(0)));
   Fraction zero2 = new Fraction(0, 2);
   assertTrue(zero.equals(zero2));
   assertEquals(zero.hashCode(), zero2.hashCode());
   Fraction one = new Fraction(1, 1);
   assertFalse((one.equals(zero) || zero.equals(one)));
 }
Exemple #15
0
  public void testSubtract() {
    Fraction a = new Fraction(1, 2);
    Fraction b = new Fraction(2, 3);

    assertFraction(0, 1, a.subtract(a));
    assertFraction(-1, 6, a.subtract(b));
    assertFraction(1, 6, b.subtract(a));
    assertFraction(0, 1, b.subtract(b));

    Fraction f = new Fraction(1, 1);
    try {
      f.subtract(null);
      fail("expecting IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
    }

    // if this fraction is subtracted naively, it will overflow.
    // check that it doesn't.
    Fraction f1 = new Fraction(1, 32768 * 3);
    Fraction f2 = new Fraction(1, 59049);
    f = f1.subtract(f2);
    assertEquals(-13085, f.getNumerator());
    assertEquals(1934917632, f.getDenominator());

    f1 = new Fraction(Integer.MIN_VALUE, 3);
    f2 = new Fraction(1, 3).negate();
    f = f1.subtract(f2);
    assertEquals(Integer.MIN_VALUE + 1, f.getNumerator());
    assertEquals(3, f.getDenominator());

    f1 = new Fraction(Integer.MAX_VALUE, 1);
    f2 = Fraction.ONE;
    f = f1.subtract(f2);
    assertEquals(Integer.MAX_VALUE - 1, f.getNumerator());
    assertEquals(1, f.getDenominator());

    try {
      f1 = new Fraction(1, Integer.MAX_VALUE);
      f2 = new Fraction(1, Integer.MAX_VALUE - 1);
      f = f1.subtract(f2);
      fail("expecting ArithmeticException"); // should overflow
    } catch (ArithmeticException ex) {
    }

    // denominator should not be a multiple of 2 or 3 to trigger overflow
    f1 = new Fraction(Integer.MIN_VALUE, 5);
    f2 = new Fraction(1, 5);
    try {
      f = f1.subtract(f2); // should overflow
      fail("expecting ArithmeticException but got: " + f.toString());
    } catch (ArithmeticException ex) {
    }

    try {
      f = new Fraction(Integer.MIN_VALUE, 1);
      f = f.subtract(Fraction.ONE);
      fail("expecting ArithmeticException");
    } catch (ArithmeticException ex) {
    }

    try {
      f = new Fraction(Integer.MAX_VALUE, 1);
      f = f.subtract(Fraction.ONE.negate());
      fail("expecting ArithmeticException");
    } catch (ArithmeticException ex) {
    }

    f1 = new Fraction(3, 327680);
    f2 = new Fraction(2, 59049);
    try {
      f = f1.subtract(f2); // should overflow
      fail("expecting ArithmeticException but got: " + f.toString());
    } catch (ArithmeticException ex) {
    }
  }
Exemple #16
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) {
    }
  }
Exemple #17
0
  public void testAdd() {
    Fraction a = new Fraction(1, 2);
    Fraction b = new Fraction(2, 3);

    assertFraction(1, 1, a.add(a));
    assertFraction(7, 6, a.add(b));
    assertFraction(7, 6, b.add(a));
    assertFraction(4, 3, b.add(b));

    Fraction f1 = new Fraction(Integer.MAX_VALUE - 1, 1);
    Fraction f2 = Fraction.ONE;
    Fraction f = f1.add(f2);
    assertEquals(Integer.MAX_VALUE, f.getNumerator());
    assertEquals(1, f.getDenominator());

    f1 = new Fraction(-1, 13 * 13 * 2 * 2);
    f2 = new Fraction(-2, 13 * 17 * 2);
    f = f1.add(f2);
    assertEquals(13 * 13 * 17 * 2 * 2, f.getDenominator());
    assertEquals(-17 - 2 * 13 * 2, f.getNumerator());

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

    // if this fraction is added naively, it will overflow.
    // check that it doesn't.
    f1 = new Fraction(1, 32768 * 3);
    f2 = new Fraction(1, 59049);
    f = f1.add(f2);
    assertEquals(52451, f.getNumerator());
    assertEquals(1934917632, f.getDenominator());

    f1 = new Fraction(Integer.MIN_VALUE, 3);
    f2 = new Fraction(1, 3);
    f = f1.add(f2);
    assertEquals(Integer.MIN_VALUE + 1, f.getNumerator());
    assertEquals(3, f.getDenominator());

    f1 = new Fraction(Integer.MAX_VALUE - 1, 1);
    f2 = Fraction.ONE;
    f = f1.add(f2);
    assertEquals(Integer.MAX_VALUE, f.getNumerator());
    assertEquals(1, f.getDenominator());

    try {
      f = f.add(Fraction.ONE); // should overflow
      fail("expecting ArithmeticException but got: " + f.toString());
    } catch (ArithmeticException ex) {
    }

    // denominator should not be a multiple of 2 or 3 to trigger overflow
    f1 = new Fraction(Integer.MIN_VALUE, 5);
    f2 = new Fraction(-1, 5);
    try {
      f = f1.add(f2); // should overflow
      fail("expecting ArithmeticException but got: " + f.toString());
    } catch (ArithmeticException ex) {
    }

    try {
      f = new Fraction(-Integer.MAX_VALUE, 1);
      f = f.add(f);
      fail("expecting ArithmeticException");
    } catch (ArithmeticException ex) {
    }

    try {
      f = new Fraction(-Integer.MAX_VALUE, 1);
      f = f.add(f);
      fail("expecting ArithmeticException");
    } catch (ArithmeticException ex) {
    }

    f1 = new Fraction(3, 327680);
    f2 = new Fraction(2, 59049);
    try {
      f = f1.add(f2); // should overflow
      fail("expecting ArithmeticException but got: " + f.toString());
    } catch (ArithmeticException ex) {
    }
  }
Exemple #18
0
  public void testNegate() {
    Fraction f = null;

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

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

    // large values
    f = new Fraction(Integer.MAX_VALUE - 1, Integer.MAX_VALUE);
    f = f.negate();
    assertEquals(Integer.MIN_VALUE + 2, f.getNumerator());
    assertEquals(Integer.MAX_VALUE, f.getDenominator());

    f = new Fraction(Integer.MIN_VALUE, 1);
    try {
      f = f.negate();
      fail("expecting ArithmeticException");
    } catch (ArithmeticException ex) {
    }
  }
Exemple #19
0
 private String fractionToString(Fraction fraction) {
   return fraction.getDenominator() == 1
       ? Integer.toString(fraction.getNumerator())
       : FractionFormat.getImproperInstance().format(fraction);
 }
Exemple #20
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 #21
0
  public void testCompareTo() {
    Fraction first = new Fraction(1, 2);
    Fraction second = new Fraction(1, 3);
    Fraction third = new Fraction(1, 2);

    assertEquals(0, first.compareTo(first));
    assertEquals(0, first.compareTo(third));
    assertEquals(1, first.compareTo(second));
    assertEquals(-1, second.compareTo(first));

    // these two values are different approximations of PI
    // the first  one is approximately PI - 3.07e-18
    // the second one is approximately PI + 1.936e-17
    Fraction pi1 = new Fraction(1068966896, 340262731);
    Fraction pi2 = new Fraction(411557987, 131002976);
    assertEquals(-1, pi1.compareTo(pi2));
    assertEquals(1, pi2.compareTo(pi1));
    assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20);
  }