Search in sources :

Example 6 with BigFloat

use of ch.obermuhlner.math.big.BigFloat in project big-math by eobermuhlner.

the class BigFloatStreamTest method testRangeStepMinus1.

@Test
public void testRangeStepMinus1() {
    Context context = BigFloat.context(20);
    List<BigFloat> list = BigFloatStream.range(context.valueOf(0), context.valueOf(10), context.valueOf(-1)).collect(Collectors.toList());
    assertList(list, 0, 0);
}
Also used : Context(ch.obermuhlner.math.big.BigFloat.Context) BigFloat(ch.obermuhlner.math.big.BigFloat) Test(org.junit.Test)

Example 7 with BigFloat

use of ch.obermuhlner.math.big.BigFloat in project big-math by eobermuhlner.

the class BigFloatStreamTest method testRangeStep3.

@Test
public void testRangeStep3() {
    Context context = BigFloat.context(20);
    List<BigFloat> list = BigFloatStream.range(context.valueOf(0), context.valueOf(10), context.valueOf(3)).collect(Collectors.toList());
    assertEquals(4, list.size());
    assertEquals(true, list.contains(context.valueOf(0)));
    assertEquals(true, list.contains(context.valueOf(3)));
    assertEquals(true, list.contains(context.valueOf(6)));
    assertEquals(true, list.contains(context.valueOf(9)));
    assertEquals(0, BigFloatStream.range(context.valueOf(0), context.valueOf(-1), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(0, BigFloatStream.range(context.valueOf(0), context.valueOf(0), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(1, BigFloatStream.range(context.valueOf(0), context.valueOf(1), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(1, BigFloatStream.range(context.valueOf(0), context.valueOf(2), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(1, BigFloatStream.range(context.valueOf(0), context.valueOf(3), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(2, BigFloatStream.range(context.valueOf(0), context.valueOf(4), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(2, BigFloatStream.range(context.valueOf(0), context.valueOf(5), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(2, BigFloatStream.range(context.valueOf(0), context.valueOf(6), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(3, BigFloatStream.range(context.valueOf(0), context.valueOf(7), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(3, BigFloatStream.range(context.valueOf(0), context.valueOf(8), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(3, BigFloatStream.range(context.valueOf(0), context.valueOf(9), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(4, BigFloatStream.range(context.valueOf(0), context.valueOf(10), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(4, BigFloatStream.range(context.valueOf(0), context.valueOf(11), context.valueOf(3)).collect(Collectors.toList()).size());
    assertEquals(4, BigFloatStream.range(context.valueOf(0), context.valueOf(12), context.valueOf(3)).collect(Collectors.toList()).size());
}
Also used : Context(ch.obermuhlner.math.big.BigFloat.Context) BigFloat(ch.obermuhlner.math.big.BigFloat) Test(org.junit.Test)

Example 8 with BigFloat

use of ch.obermuhlner.math.big.BigFloat in project big-math by eobermuhlner.

the class BigFloatStreamTest method testRangeClosedDoubleStep3.

@Test
public void testRangeClosedDoubleStep3() {
    Context context = BigFloat.context(20);
    List<BigFloat> list = BigFloatStream.rangeClosed(0.0, 12.0, 3.0, context).collect(Collectors.toList());
    assertEquals(5, list.size());
    assertEquals(true, list.contains(context.valueOf(0)));
    assertEquals(true, list.contains(context.valueOf(3)));
    assertEquals(true, list.contains(context.valueOf(6)));
    assertEquals(true, list.contains(context.valueOf(9)));
    assertEquals(true, list.contains(context.valueOf(12)));
}
Also used : Context(ch.obermuhlner.math.big.BigFloat.Context) BigFloat(ch.obermuhlner.math.big.BigFloat) Test(org.junit.Test)

Example 9 with BigFloat

use of ch.obermuhlner.math.big.BigFloat in project big-math by eobermuhlner.

the class BigFloatStreamTest method testRangeClosedStepMinus1.

@Test
public void testRangeClosedStepMinus1() {
    Context context = BigFloat.context(20);
    List<BigFloat> list = BigFloatStream.rangeClosed(context.valueOf(0), context.valueOf(10), context.valueOf(-1)).collect(Collectors.toList());
    assertList(list, 0, 0);
}
Also used : Context(ch.obermuhlner.math.big.BigFloat.Context) BigFloat(ch.obermuhlner.math.big.BigFloat) Test(org.junit.Test)

Example 10 with BigFloat

use of ch.obermuhlner.math.big.BigFloat in project big-math by eobermuhlner.

the class BigFloatExample method piChudnovski.

private static BigFloat piChudnovski(int precision) {
    Context context = BigFloat.context(precision + 10);
    final BigFloat valueDivisor = context.valueOf(640320).pow(3).divide(24);
    BigFloat sumA = context.valueOf(1);
    BigFloat sumB = context.valueOf(0);
    BigFloat a = context.valueOf(1);
    // -(6*k - 5)
    BigFloat dividendTerm1 = context.valueOf(5);
    // 2*k - 1
    BigFloat dividendTerm2 = context.valueOf(-1);
    // 6*k - 1
    BigFloat dividendTerm3 = context.valueOf(-1);
    long iterationCount = (context.getPrecision() + 13) / 14;
    for (long k = 1; k <= iterationCount; k++) {
        BigFloat valueK = context.valueOf(k);
        dividendTerm1 = dividendTerm1.add(-6);
        dividendTerm2 = dividendTerm2.add(2);
        dividendTerm3 = dividendTerm3.add(6);
        BigFloat dividend = dividendTerm1.multiply(dividendTerm2).multiply(dividendTerm3);
        BigFloat kPower3 = valueK.pow(3);
        BigFloat divisor = kPower3.multiply(valueDivisor);
        a = a.multiply(dividend).divide(divisor);
        BigFloat b = valueK.multiply(a);
        sumA = sumA.add(a);
        sumB = sumB.add(b);
    }
    final BigFloat factor = sqrt(context.valueOf(10005)).multiply(426880);
    BigFloat pi = factor.divide(sumA.multiply(13591409).add(sumB.multiply(545140134)));
    return context(precision).valueOf(pi);
}
Also used : Context(ch.obermuhlner.math.big.BigFloat.Context) BigFloat(ch.obermuhlner.math.big.BigFloat)

Aggregations

BigFloat (ch.obermuhlner.math.big.BigFloat)15 Context (ch.obermuhlner.math.big.BigFloat.Context)15 Test (org.junit.Test)14 MathContext (java.math.MathContext)1