The Taylor series for
sin(x)
is simple:
sin(x) = pow(x, 1) - pow(x, 3) / 3! + pow(x, 5) / 5! - ...
But your implementation does two things: starts with -1 instead of 1, and makes it look a lot more complex than it needs to be. If you don't believe me, work out each term in a short series manually (4 or 5 terms should do) and use the debugger to look at the equivalent terms your code is generating!
Here's a simple implementation in C#:
static double sin(double x)
{
double result = 0.0;
double sign = 1.0;
for (double power = 1.0; power <= 20.0; power += 2, sign = -sign)
{
result += sign * (Math.Pow(x, power) / factorial(power));
}
return result;
}
static double factorial(double n)
{
double result = 1.0;
while (n > 1.0)
{
result *= n;
n -= 1.0;
}
return result;
}
Which gives a result accurate to at least 5 decimal places despite using a much, much shorter series that you do!
You may also need to remember when testing that the Taylor series works in Radians, not Degrees!