/** * pi Series * -- this is based on pi/4 = 1 - (1/3) + (1/5) - (1/7) +... * -- this is the straightforward version */ public class pi1 extends series { protected int sign = 1; protected double valpos = 0.0; protected double valneg = 0.0; /** * Constructor for Pi Series * * @param x is the argument for the series */ pi1(double x){ param = x; n = -1; term = 0.0; valpos = 0.0; valneg = 0.0; val = valpos; } /** * First initializes the first term of series */ public void first(){ term=1.0; sign=1; n=0; valpos = term; valneg = 0.0; val = valpos - valneg; } /** * Advances to the next term in the series. */ public void nextTerm(){ n++; sign = -1*sign; term = 1.0/(2*n+1.0); if (sign>0) valpos = valpos + term; else valneg = valneg + term; val = val + sign*term; // inaccurate way // val = valpos - valneg; // accurate way // System.out.println("n=" + n + ", sign=" + sign + ", term=" +term); } /** * Returns the value of the mth term in series * * @param m the index of a term */ public double valAt(int m){ first(); for (int i=0; i