Interest Ask. Like Tatarize, I hope this is not an evasion of homework.
This code predicts the number of terms needed to obtain absolute accuracy +/- 0.000 000 1 as a result for all angles 0 - 90 degrees.
The highest power term distinguishes x ^ k / k! to the result. So,
x^k / k! < 1 / 10^7
Here x is in radians, so the largest value of x is ~ 1.57 rad. This means that only a series up to capacity 13 will give you a final term of less than 0.000 000 1.
Unfortunately, my computer has an advanced age (32-bit) and any attempt to calculate 13! causes overflow. Therefore, I am adapting the Horner method a bit, possibly losing some efficiency, but avoiding factorial overflow and allowing to stop if the angle is small or if adequate accuracy is reached up to a power of 13.
Sin x = x - x^2(x/3! - x^2(x/5! - x^2(x/7! - . . . - x^2(x/(m-1)!- x^2(x/m!)
where m is the highest power required for the required absolute accuracy.
Sin x = x + Sum { iTerm(i) * x^2 / (i * (i-1)) }
Where
iTerm(0) = x and iTerm(n) = - x^2 * iTerm(n-1)/(i*(i-1)
PS - Why can't we use Math formatting outside of the Exchange Mathematics Stack Exchange? This would greatly simplify the written equations.
public class TrigByPoly { // No constructor used. public static void main(String[] args) { double x0 = 0, x1 = Math.PI/12, x2 = Math.PI/6, x3 = Math.PI/4, x4 = Math.PI/3, x5 = Math.PI/2; double sinx0 = SinByPoly(x0), sinx1 = SinByPoly(x1), sinx2 = SinByPoly(x2), sinx3 = SinByPoly(x3), sinx4 = SinByPoly(x4), sinx5 = SinByPoly(x5); System.out.println("Sin(0) to 7 decimal places is : " + sinx0); System.out.println("Sin(15) to 7 decimal places is : " + sinx1); System.out.println("Sin(30) to 7 decimal places is : " + sinx2); System.out.println("Sin(45) to 7 decimal places is : " + sinx3); System.out.println("Sin(60) to 7 decimal places is : " + sinx4); System.out.println("Sin(90) to 7 decimal places is : " + sinx5); } public static double SinByPoly(double x) { int i = 0; // Polynomial order indicator. double x2 = x * x, iTerm, sinx = 0; if (x < 0.0084) // Limiting angle for Sinx = x to 10^-7 precision. sinx = x; else { sinx = x; iTerm = sinx; i = 3; do { iTerm = - x2 * iTerm / ( i * (i - 1)); sinx += iTerm; i = i + 2; } while (i < 14 && (iTerm > 0.0000001 || -iTerm > 0.0000001)); } return sinx; } } OUTPUT ====== Sin(0) to an absolute precision of 1.0E-7 is : 0.0 Sin(15) to an absolute precision of 1.0E-7 is : 0.2588190618109834 Sin(30) to an absolute precision of 1.0E-7 is : 0.4999999918690232 Sin(45) to an absolute precision of 1.0E-7 is : 0.7071067829368671 Sin(60) to an absolute precision of 1.0E-7 is : 0.8660254450997811 Sin(75) to an absolute precision of 1.0E-7 is : 0.9659258210120795 Sin(90) to an absolute precision of 1.0E-7 is : 0.999999943741051