I am writing a sine function that should be recursive. I wrote a sinusoidal function, but I'm not sure how to do this recursively. Can someone explain how to start with this?
This is what I still have:
/*--------------------------------------------------------------
Name: sine( double X );
Return: Function "sine" will return the
sine of X, where X is measured in radians.
--------------------------------------------------------------*/
double sine(double X)
{
double result = 0;
double term;
int k;
double lim;
k = 0;
lim = power(10, -8);
term = power(-1, k)*power(X, ((2*k) + 1)) / (factorial((2*k)+1));
result = term;
while (absolute(term) > lim)
{
k += 1;
term = power(-1, k)*power(X, ((2*k) + 1)) / (factorial((2*k)+1));
result += term;
}
return result;
}
EDIT: I used the wrapper function to solve this problem. Basically created another function called
double sine_rec(double X, double k)
and changed around the current code to fit this.
source
share