// Big_math.java generate Pi, e to 100 fraction bits (error<1/2^100) // and a few other sample computations and functions import java.math.BigDecimal; public class Big_math { BigDecimal epsilon; // based on desired precision BigDecimal natural_e; BigDecimal pi, npi; BigDecimal twopi; BigDecimal halfpi; int prec = 100; // digits int nbits = 332; // precision in bits = about 3.32 precision in digits public Big_math() // constructor { // System.out.println("Big_math"); // "constants" needed by other functions and users natural_e = naturalE(nbits); /* 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_math"); } 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(nbits,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