All pastes #1340096 Raw Edit

PiSalaminBrent

public java v1 · immutable
#1340096 ·published 2009-02-17 15:25 UTC
rendered paste body
import java.io.IOException;import java.util.Scanner;import org.apfloat.ApfloatMath;import org.apfloat.Apfloat;/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author Jake Gunter */public class PiSalaminBrent {    private long precision = 1000;    private Apfloat[] calculateNums(Apfloat[] lastIter, int iter, int desiredIter) {        Apfloat[] toReturn = new Apfloat[4];        toReturn[0] = (lastIter[0].add(lastIter[1])).divide(new Apfloat(2));        toReturn[1] = ApfloatMath.sqrt(lastIter[0].multiply(lastIter[1]));        Apfloat x = ApfloatMath.pow((lastIter[0].subtract(toReturn[0])), 2);        toReturn[2] = lastIter[2].subtract(lastIter[3].multiply(x));        toReturn[3] = lastIter[3].multiply(new Apfloat(2));        if (iter == desiredIter) return toReturn;        return calculateNums(toReturn, ++iter, desiredIter);    }        private Apfloat calculate2(Apfloat a, int iter, int desiredIter) {        a = a.subtract(ApfloatMath.tan(a));        if (iter == desiredIter) return a;        return calculate2(a, iter+1, desiredIter);    }        private Apfloat calculateNonic(Apfloat a, int iter, int desiredIter) {        Apfloat firstTerm = ApfloatMath.tan(a);        Apfloat secondTerm = (new Apfloat(1, precision).divide(new Apfloat(3, precision))).multiply(ApfloatMath.pow(ApfloatMath.tan(a), 3));        Apfloat thirdTerm = (new Apfloat(1, precision).divide(new Apfloat(5, precision))).multiply(ApfloatMath.pow(ApfloatMath.tan(a), 5));        Apfloat fourthTerm = (new Apfloat(1, precision).divide(new Apfloat(7, precision))).multiply(ApfloatMath.pow(ApfloatMath.tan(a), 7));        return ApfloatMath.sum(a, firstTerm.negate(), secondTerm, thirdTerm.negate(), fourthTerm);    }        private Apfloat calculateVeryVerySimplest(long precision) {        return new Apfloat(4, precision).multiply(ApfloatMath.atan(new Apfloat(1, precision)));    }        private Apfloat calculateSimplest(Apfloat curIter, int iter, int desiredIter) {        Apfloat toReturn = curIter.add(ApfloatMath.sin(curIter));        if (iter == desiredIter) return toReturn;        return calculateSimplest(toReturn, ++iter, desiredIter);    }        private Apfloat calculateSimpler(long precision) {        Apfloat a = Apfloat.ONE.divide(ApfloatMath.sqrt(new Apfloat(3, precision)));        a = ApfloatMath.atan(a);        return a.multiply(new Apfloat(6, precision));    }        private Apfloat calculateMachinLikeSimple(long precision) {        Apfloat oneFifth = Apfloat.ONE.divide(new Apfloat(5, precision));        Apfloat oneTwoThirtyNinth = Apfloat.ONE.divide(new Apfloat(239, precision));        Apfloat pi = (new Apfloat(4, precision).multiply(ApfloatMath.atan(oneFifth))).subtract(ApfloatMath.atan(oneTwoThirtyNinth));        return pi.multiply(new Apfloat(4, precision));    }        public static void main(String[] args) throws IOException {        long precision = 10000;        Scanner in = new Scanner(System.in);        do {            System.out.println("\nPI CALCULATOR, V. 3.0");            System.out.println("----------------------------------");            System.out.print("How many digits? Type in a number less than 1 to exit. ");            precision = in.nextLong();            /*            Apfloat a = Apfloat.ZERO;            Apfloat b = Apfloat.ONE.divide(ApfloatMath.sqrt(new Apfloat(2)));            Apfloat c = new Apfloat(.25);            Apfloat d = Apfloat.ONE;            Apfloat[] pi = new PiSalaminBrent().calculateNums(new Apfloat[] {a, b, c, d}, 0, 3);            Apfloat num = ApfloatMath.pow(pi[0].add(pi[1]), 2);            Apfloat den = new Apfloat(4).multiply(pi[3]);            System.out.println((num.divide(den)).toString(true));*/            if (precision > 0) {                long start = System.currentTimeMillis();                Apfloat pi = new PiSalaminBrent().//calculateSimplest(new Apfloat(22, precision).divide(new Apfloat(7, precision)), 0, 2);                        //calculateSimpler(precision);                        //calculate2(new Apfloat(22, precision).divide(new Apfloat(7, precision)), 0, 10);                        //calculateVeryVerySimplest(precision);                        calculateMachinLikeSimple(precision);                long total = System.currentTimeMillis() - start;                System.out.println(pi.toString(true));                System.out.println("Calculation took " + total + " milliseconds.");            }        } while (precision > 0);            }}