Search in sources :

Example 61 with Context

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

the class BigFloatTest method testE.

@Test
public void testE() {
    Context context = context(MathContext.DECIMAL32);
    assertEquals(BigDecimalMath.e(context.getMathContext()), context.e().toBigDecimal());
}
Also used : MathContext(java.math.MathContext) Context(ch.obermuhlner.math.big.BigFloat.Context) Test(org.junit.Test)

Example 62 with Context

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

the class BigFloatTest method testAtanh.

@Test
public void testAtanh() {
    Context context = context(MathContext.DECIMAL32);
    assertEquals(atanh(context.valueOf(0)).toBigDecimal(), BigDecimalMath.atanh(BigDecimal.valueOf(0), MathContext.DECIMAL32));
}
Also used : MathContext(java.math.MathContext) Context(ch.obermuhlner.math.big.BigFloat.Context) Test(org.junit.Test)

Example 63 with Context

use of ch.obermuhlner.math.big.BigFloat.Context 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)

Example 64 with Context

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

the class BigFloatStreamExample method main.

public static void main(String[] args) {
    Context context = BigFloat.context(20);
    System.out.println("Range [0, 10) step 1 (using BigDecimal as input parameters)");
    BigFloatStream.range(context.valueOf(0), context.valueOf(10), context.valueOf(1)).forEach(System.out::println);
    System.out.println();
    System.out.println("Range [0, 12] step 3 (using long as input parameters)");
    BigFloatStream.rangeClosed(0, 12, 3, context).forEach(System.out::println);
    System.out.println();
    System.out.println("Range [0, 12] step 3 (using double as input parameters)");
    BigFloatStream.rangeClosed(0.0, 12.0, 3.0, context).forEach(System.out::println);
    System.out.println();
    System.out.println("Range [10, 0) step -1");
    BigFloatStream.range(10, 0, -1, context).forEach(System.out::println);
    System.out.println();
    System.out.println("Range [10, 0] step -1");
    BigFloatStream.rangeClosed(10, 0, -1, context).forEach(System.out::println);
    System.out.println();
    System.out.println("Range [0, 10] step 1 parallel");
    BigFloatStream.rangeClosed(0, 10, 1, context).parallel().forEach(System.out::println);
    System.out.println();
}
Also used : Context(ch.obermuhlner.math.big.BigFloat.Context)

Example 65 with Context

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

the class BigFloatTest method testGetFractionalPart.

@Test
public void testGetFractionalPart() {
    Context context = context(MathContext.DECIMAL32);
    assertEquals(context.valueOf(0.456), context.valueOf(123.456).getFractionalPart());
}
Also used : MathContext(java.math.MathContext) Context(ch.obermuhlner.math.big.BigFloat.Context) Test(org.junit.Test)

Aggregations

Context (ch.obermuhlner.math.big.BigFloat.Context)78 Test (org.junit.Test)76 MathContext (java.math.MathContext)61 BigFloat (ch.obermuhlner.math.big.BigFloat)15