BigRational implements Comparable<BigRational> { public static BigRational bernoulli(int n) { if (n < 0) { throw new ArithmeticException("Illegal bernoulli(n) for n < 0: n = " + n); } if (n == 1) { return valueOf(-1, 2); } else if (n % 2 == 1) { return ZERO; } synchronized (bernoulliCache) { int index = n / 2; if (bernoulliCache.size() <= index) { for (int i = bernoulliCache.size(); i <= index; i++) { BigRational b = calculateBernoulli(i * 2); bernoulliCache.add(b); } } return bernoulliCache.get(index); } } private  BigRational(int value); private  BigRational(BigDecimal num, BigDecimal denom); BigInteger getNumeratorBigInteger(); BigDecimal getNumerator(); BigInteger getDenominatorBigInteger(); BigDecimal getDenominator(); BigRational reduce(); BigRational integerPart(); BigRational fractionPart(); BigRational negate(); BigRational reciprocal(); BigRational abs(); int signum(); BigRational increment(); BigRational decrement(); BigRational add(BigRational value); BigRational add(BigInteger value); BigRational add(int value); BigRational subtract(BigRational value); BigRational subtract(BigInteger value); BigRational subtract(int value); BigRational multiply(BigRational value); BigRational multiply(BigInteger value); BigRational multiply(int value); BigRational divide(BigRational value); BigRational divide(BigInteger value); BigRational divide(int value); boolean isZero(); boolean isInteger(); BigRational pow(int exponent); BigRational withPrecision(int precision); BigRational withScale(int scale); double toDouble(); float toFloat(); BigDecimal toBigDecimal(); BigDecimal toBigDecimal(MathContext mc); @Override int compareTo(BigRational other); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); String toPlainString(); String toRationalString(); String toIntegerRationalString(); static BigRational valueOf(int value); static BigRational valueOf(int numerator, int denominator); static BigRational valueOf(int integer, int fractionNumerator, int fractionDenominator); static BigRational valueOf(BigInteger numerator, BigInteger denominator); static BigRational valueOf(BigInteger value); static BigRational valueOf(double value); static BigRational valueOf(BigDecimal value); static BigRational valueOf(String string); static BigRational valueOf(boolean positive, String integerPart, String fractionPart, String fractionRepeatPart, String exponentPart); static BigRational valueOf(BigDecimal numerator, BigDecimal denominator); static BigRational min(BigRational... values); static BigRational max(BigRational... values); static BigRational bernoulli(int n); static final BigRational ZERO; static final BigRational ONE; static final BigRational TWO; static final BigRational TEN; }
@Test public void testBernoulli() { assertEquals("1", bernoulli(0).reduce().toRationalString()); assertEquals("-1/2", bernoulli(1).reduce().toRationalString()); assertEquals("1/6", bernoulli(2).reduce().toRationalString()); assertEquals("0", bernoulli(3).reduce().toRationalString()); assertEquals("-1/30", bernoulli(4).reduce().toRationalString()); assertEquals("0", bernoulli(5).reduce().toRationalString()); assertEquals("1/42", bernoulli(6).reduce().toRationalString()); assertEquals("0", bernoulli(7).reduce().toRationalString()); assertEquals("-1/30", bernoulli(8).reduce().toRationalString()); assertEquals("0", bernoulli(9).reduce().toRationalString()); assertEquals("5/66", bernoulli(10).reduce().toRationalString()); assertEquals("0", bernoulli(11).reduce().toRationalString()); assertEquals("-691/2730", bernoulli(12).reduce().toRationalString()); assertEquals("0", bernoulli(13).reduce().toRationalString()); assertEquals("7/6", bernoulli(14).reduce().toRationalString()); assertEquals("0", bernoulli(15).reduce().toRationalString()); assertEquals("-3617/510", bernoulli(16).reduce().toRationalString()); assertEquals("0", bernoulli(17).reduce().toRationalString()); assertEquals("43867/798", bernoulli(18).reduce().toRationalString()); }
@Test(expected = ArithmeticException.class) public void testBernoulliNegative() { bernoulli(-1); }