
/**
 * sine function
 */
public class sine extends series 
{
	protected int sign = 1;
	protected double valpos = 0.0;
	protected double valneg = 0.0;
	
	sine(double x){
		param = x;
		n = -1;
		term = 0.0;
		valpos = 0.0;  valneg = 0.0;
		val = valpos;
	}
	public void first(){
		term=param;
		sign=1;
		n=0;
		valpos = term; valneg = 0.0;
		val = valpos - valneg;
	}
	
	public void nextTerm(){
		n++;
		sign = -1*sign;
		term = term*(param*param)/((2*n)*(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);
	}	
	
	public double valAt(int m){
		first();
		for (int i=0; i<m; i++)
			nextTerm();
		return val;
	}
	
}
