BigFraction extends Number implements FieldElement<BigFraction>, Comparable<BigFraction>, Serializable { @Override public double doubleValue() { double result = numerator.doubleValue() / denominator.doubleValue(); if (Double.isNaN(result)) { int shift = FastMath.max(numerator.bitLength(), denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE); result = numerator.shiftRight(shift).doubleValue() / denominator.shiftRight(shift).doubleValue(); } return result; } BigFraction(final BigInteger num);  BigFraction(BigInteger num, BigInteger den);  BigFraction(final double value);  BigFraction(final double value, final double epsilon,
                       final int maxIterations); private  BigFraction(final double value, final double epsilon,
                        final int maxDenominator, int maxIterations);  BigFraction(final double value, final int maxDenominator);  BigFraction(final int num);  BigFraction(final int num, final int den);  BigFraction(final long num);  BigFraction(final long num, final long den); static BigFraction getReducedFraction(final int numerator,
                                                 final int denominator); BigFraction abs(); BigFraction add(final BigInteger bg); BigFraction add(final int i); BigFraction add(final long l); BigFraction add(final BigFraction fraction); BigDecimal bigDecimalValue(); BigDecimal bigDecimalValue(final int roundingMode); BigDecimal bigDecimalValue(final int scale, final int roundingMode); int compareTo(final BigFraction object); BigFraction divide(final BigInteger bg); BigFraction divide(final int i); BigFraction divide(final long l); BigFraction divide(final BigFraction fraction); @Override double doubleValue(); @Override boolean equals(final Object other); @Override float floatValue(); BigInteger getDenominator(); int getDenominatorAsInt(); long getDenominatorAsLong(); BigInteger getNumerator(); int getNumeratorAsInt(); long getNumeratorAsLong(); @Override int hashCode(); @Override int intValue(); @Override long longValue(); BigFraction multiply(final BigInteger bg); BigFraction multiply(final int i); BigFraction multiply(final long l); BigFraction multiply(final BigFraction fraction); BigFraction negate(); double percentageValue(); BigFraction pow(final int exponent); BigFraction pow(final long exponent); BigFraction pow(final BigInteger exponent); double pow(final double exponent); BigFraction reciprocal(); BigFraction reduce(); BigFraction subtract(final BigInteger bg); BigFraction subtract(final int i); BigFraction subtract(final long l); BigFraction subtract(final BigFraction fraction); @Override String toString(); BigFractionField getField(); static final BigFraction TWO; static final BigFraction ONE; static final BigFraction ZERO; static final BigFraction MINUS_ONE; static final BigFraction FOUR_FIFTHS; static final BigFraction ONE_FIFTH; static final BigFraction ONE_HALF; static final BigFraction ONE_QUARTER; static final BigFraction ONE_THIRD; static final BigFraction THREE_FIFTHS; static final BigFraction THREE_QUARTERS; static final BigFraction TWO_FIFTHS; static final BigFraction TWO_QUARTERS; static final BigFraction TWO_THIRDS; }
@Test public void testConstructor() { assertFraction(0, 1, new BigFraction(0, 1)); assertFraction(0, 1, new BigFraction(0l, 2l)); assertFraction(0, 1, new BigFraction(0, -1)); assertFraction(1, 2, new BigFraction(1, 2)); assertFraction(1, 2, new BigFraction(2, 4)); assertFraction(-1, 2, new BigFraction(-1, 2)); assertFraction(-1, 2, new BigFraction(1, -2)); assertFraction(-1, 2, new BigFraction(-2, 4)); assertFraction(-1, 2, new BigFraction(2, -4)); assertFraction(11, 1, new BigFraction(11)); assertFraction(11, 1, new BigFraction(11l)); assertFraction(11, 1, new BigFraction(new BigInteger("11"))); assertFraction(0, 1, new BigFraction(0.00000000000001, 1.0e-5, 100)); assertFraction(2, 5, new BigFraction(0.40000000000001, 1.0e-5, 100)); assertFraction(15, 1, new BigFraction(15.0000000000001, 1.0e-5, 100)); Assert.assertEquals(0.00000000000001, new BigFraction(0.00000000000001).doubleValue(), 0.0); Assert.assertEquals(0.40000000000001, new BigFraction(0.40000000000001).doubleValue(), 0.0); Assert.assertEquals(15.0000000000001, new BigFraction(15.0000000000001).doubleValue(), 0.0); assertFraction(3602879701896487l, 9007199254740992l, new BigFraction(0.40000000000001)); assertFraction(1055531162664967l, 70368744177664l, new BigFraction(15.0000000000001)); try { new BigFraction(null, BigInteger.ONE); Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException npe) { } try { new BigFraction(BigInteger.ONE, null); Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException npe) { } try { new BigFraction(BigInteger.ONE, BigInteger.ZERO); Assert.fail("Expecting ZeroException"); } catch (ZeroException npe) { } try { new BigFraction(2.0 * Integer.MAX_VALUE, 1.0e-5, 100000); Assert.fail("Expecting FractionConversionException"); } catch (FractionConversionException fce) { } }
@Test public void testDoubleValue() { BigFraction first = new BigFraction(1, 2); BigFraction second = new BigFraction(1, 3); Assert.assertEquals(0.5, first.doubleValue(), 0.0); Assert.assertEquals(1.0 / 3.0, second.doubleValue(), 0.0); }