Was wondering if someone could offer me some advice..
I am trying to evaluate sin(x) using the recurrence relation sin(ny) = 2cos(y)sin((n-1)y)-sin((n-2)y).
The problem states that i take y = 0.01 and cos(y) = 1 - x^2/2, and obviously for a small x, sinx = x.
I have so far attempted to do this using the function below, however the results it provides are either wrong, or the program gets stuck repeatedly calling itself. I think the problem may well lie in my criteria for if x is small, but i can't figure out a another way.
So i ask: Does this seem likely? Should i start over? What stupid thing(s) am i doing?
thanks a lot
Code:(meanwhile in main...)
result = sin_recur(n, y);
...
double sin_recur(double n, double y)
{
double x = n*y;
if( x <= 0.0001 && x >= -0.0001) //for small x, sin(x) = x
return x;
else if(x == 0.0) //sin(0) = 0
return 0;
else
return (2 - (x*x))*(sin_recur(n-1, y)) - sin_recur(n-2, y) ; //use recurrence relation
}