The value of sin(x) can be approximated by using the formula:
sin(x) = x −x3/3!+x5/5!−x7/7!+x9/9!· · ·
Using this formula, determine how many terms are needed to approximate the value returned by the intrinsic sin() function with an error less than 1 e-6, when x=30 degrees.(Hints: Use a while loop that terminates when the difference between the value returned by the intrinsic sin() function and the approximation is less than 1 e-6. Also note that X must first be converted to radian measure and that the alternating sign in the approximating series can be determined as (-1)*(n+1) where n is the number of terms used in the approximation.
If you would have to calculate the sum of numbers 1,2,3,..,n -- how would you do that?
If you would have to calculate the product of numbers 1,2,3,..,n -- how would you do that?
Seems like homework to me so I'll pass on explaining
If you want to make your own sin function and speed it up make a wavetable and calculate n Values (depending on the need of accuracy, 1024 should be enough for most tasks)
The hint pretty much just tells you how to solve the problem. First, calculate the value of sin(30) using the sin function from <cmath> (remember to convert degrees to radians). Then you can use a while loop to approximate the series that they gave you. I'll give you a basic outline to start with.
#include <cmath>
#include <iostream>
usingnamespace std;
double factorial(int n)
{
//I'll leave this for you to figure out, Google is your friend if you're stuck
}
double toRadians(int deg)
{
return ((double)deg)/180*3.14152695359;
}
int main()
{
double actual = sin(toRadians(30));
double approx = 0;
int termsAdded = 0;
double termCoefficent = 1; //part of the formula
while (/*some condition to check if actual and approx are within e-6 of each other*/)
{
//something here to add the the value of the term using the termCoefficient variable and factorial function
termCoefficient += 2;
termsAdded++;
}
cout << "It took " << termsAdded << " terms to approximate sin(30) to a tolerance of 0.000001\n";
return 0;
}
That should give you a good start with the structure and general idea of what is needed.