package ch.obermuhlner.math.big.example;

import static ch.obermuhlner.math.big.BigFloat.*;

import ch.obermuhlner.math.big.BigFloat;
import ch.obermuhlner.math.big.BigFloat.Context;

public class BigFloatExample {

	public static void main(String[] args) {
		examplePi();
	}

	private static void examplePi() {
		System.out.println(piChudnovski(10));
	}

	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);
		long dividendTerm1 = 5; // -(6*k - 5)
		long dividendTerm2 = -1; // 2*k - 1
		long dividendTerm3 = -1; // 6*k - 1
		BigFloat kPower3 = context.valueOf(0);
		
		long iterationCount = (context.getPrecision()+13) / 14;
		for (long k = 1; k <= iterationCount; k++) {
			BigFloat valueK = context.valueOf(k);
			dividendTerm1 += -6;
			dividendTerm2 += 2;
			dividendTerm3 += 6;
			BigFloat dividend = context.valueOf(dividendTerm1).multiply(dividendTerm2).multiply(dividendTerm3);
			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 value426880 = context.valueOf(426880);
		final BigFloat value10005 = context.valueOf(10005);
		final BigFloat factor = value426880.multiply(sqrt(value10005));
		BigFloat pi = factor.divide(sumA.multiply(13591409).add(sumB.multiply(545140134)));
		
		return pi.withContext(context(precision));
	}
}

@@ -671,28 +671,32 @@
 ueOf(1);%0D%0A%09%09
-long
+BigFloat
  dividendTer
@@ -700,17 +700,34 @@
 Term1 = 
-5
+context.valueOf(5)
 ; // -(6
@@ -733,28 +733,32 @@
 6*k - 5)%0D%0A%09%09
-long
+BigFloat
  dividendTer
@@ -762,18 +762,35 @@
 Term2 = 
+context.valueOf(
 -1
+)
 ; // 2*k
@@ -797,20 +797,24 @@
  - 1%0D%0A%09%09
-long
+BigFloat
  dividen
@@ -822,18 +822,35 @@
 Term3 = 
+context.valueOf(
 -1
+)
 ; // 6*k
@@ -1071,13 +1071,31 @@
 rm1 
-+
 = 
+dividendTerm1.add(
 -6
+)
 ;%0D%0A%09
@@ -1114,12 +1114,30 @@
 rm2 
-+
 = 
-2
+dividendTerm2.add(2)
 ;%0D%0A%09
@@ -1156,12 +1156,30 @@
 rm3 
-+
 = 
-6
+dividendTerm3.add(6)
 ;%0D%0A%09
@@ -1531,140 +1531,40 @@
 oat 
-value426880 = context.valueOf(426880);%0D%0A%09%09final BigFloat value10005 = context.valueOf(10005);%0D%0A%09%09final BigFloat factor = value
+factor = context.valueOf(
 426880
+)
 .mul
@@ -1578,20 +1578,32 @@
 qrt(
+context.
 value
+Of(
 10005))
+)
 ;%0D%0A%09

use BigFloat where possible in piChudnovski()