// Big_pi.java generate Pi, e to 1000 fraction bits (error<1/2^1000) // and a few other sample computations and functions import java.math.BigDecimal; class Big_pi { BigDecimal epsilon; // based on desired precision BigDecimal natural_e; BigDecimal pi; int prec = 301; // precision in digits int bits = 1000; // precision in bits = about 3.32 precision in digits Big_pi() // constructor { BigDecimal a = new BigDecimal("0.12345678901234567890"); BigDecimal b = new BigDecimal("0.2345678901234567890"); BigDecimal c; BigDecimal npi; int test_int = 7; BigDecimal n_test_int = new BigDecimal(test_int);; double test_double = 1.0/3.0; BigDecimal n_test_double = new BigDecimal(test_double);; System.out.println("Big_pi"); // "constants" needed by other functions natural_e = naturalE(prec); /* precision */ epsilon = new BigDecimal("1"); for(int i=0; i=0) { System.out.print("1"); pi4 = pi4.subtract(new BigDecimal("1")); } else { System.out.print("0"); } } System.out.println(); } System.out.println(); System.out.println("fraction of e as bits"); e1 = natural_e.subtract(new BigDecimal("2")); // make fraction for(int j=0; j<20; j++) { for(int i=0; i<50; i++) { e1 = e1.multiply(new BigDecimal("2")); if(e1.compareTo(new BigDecimal("1"))>=0) { System.out.print("1"); e1 = e1.subtract(new BigDecimal("1")); } else { System.out.print("0"); } } System.out.println(); } System.out.println(); // exp(Pi*sqrt(163)) integer ? BigDecimal psqrt = npi.multiply(sqrt(new BigDecimal("163"))); psqrt = psqrt.setScale(prec,BigDecimal.ROUND_DOWN); BigDecimal iq = exp(psqrt); iq = iq.setScale(prec,BigDecimal.ROUND_DOWN); System.out.println("e^Pi*sqrt(163) ="+iq); System.out.println(); System.out.println("end Big_pi"); } BigDecimal factorial(BigDecimal n) { if(n.compareTo(new BigDecimal("1"))<=0) return new BigDecimal("1"); return n.multiply(factorial(n.subtract(new BigDecimal("1")))); } BigDecimal sqrt(BigDecimal x) { BigDecimal y = new BigDecimal("1"); BigDecimal yn = y; BigDecimal xs = x.add(epsilon); for(int i=0; i<25; i++) { yn = (y.add(xs.divide(y,BigDecimal.ROUND_DOWN))).multiply(new BigDecimal("0.5")); yn = yn.setScale(prec,BigDecimal.ROUND_DOWN); if(((yn.subtract(y)).abs()).compareTo(epsilon)<=0) return yn; y = yn; } return yn; } BigDecimal exp_series(BigDecimal x) // abs(x)<=0.5, prec digits { // prec digits returned BigDecimal fact = new BigDecimal("1"); // factorial BigDecimal xp = new BigDecimal("1"); // power of x BigDecimal y = new BigDecimal("1"); // sum of series on x int n; n = (2*prec)/3; for(int i=1; i0) // positive { while(x.compareTo(j.add(one))>0) { ep = ep.multiply(natural_e); j = j.add(one); } xc = x.subtract(j); y = ep.multiply(exp_series(xc)); y = y.setScale(prec,BigDecimal.ROUND_DOWN); return y; } else // negative { xp = x.negate(); while(xp.compareTo(j.add(one))>0) { ep = ep.multiply(natural_e); j = j.add(one); } xc = xp.subtract(j); y = ep.multiply(exp_series(xc)); y = y.setScale(prec,BigDecimal.ROUND_DOWN); return (one.add(epsilon)).divide(y, BigDecimal.ROUND_DOWN); } } // end exp BigDecimal sin(BigDecimal x) { BigDecimal y; BigDecimal xc; BigDecimal tpi = pi.multiply(new BigDecimal("2")); if(x.abs().compareTo(tpi) <0) return sin_series(x); xc = x; if(xc.compareTo(new BigDecimal("0"))>0) // positive { while(xc.compareTo(tpi)>0) { xc = xc.subtract(tpi); } y = sin_series(xc); y = y.setScale(prec,BigDecimal.ROUND_DOWN); return y; } else // negative { while(xc.compareTo(tpi)<0) { xc = xc.add(tpi); } y = sin_series(xc); return y.setScale(prec,BigDecimal.ROUND_DOWN); } } // end sin BigDecimal sin_series(BigDecimal x) // abs(x)<=0.5, prec digits { // prec digits returned BigDecimal fact = new BigDecimal("1"); // factorial BigDecimal xp = x; // power of x BigDecimal y = x; // sum of series on x int n; n = (2*prec)/3; for(int i=3; i