I presume you meant
if you want a nominal sine series. However, this is a dire method and isn't going to produce anything meaningful in your case, as the different terms are being worked out at different values of x.
First of all, PLEASE USE CODE TAGS. Your code is basically unreadable.
It should be
A more proactive compiler would prevent your code from running.
The biggest problem of all is that you fall into the trap of so many in trying to sum a power series by working out each term from scratch according to the formula written down on paper.
is ridiculous. Just multiply the previous term
Similarly, working out afresh
is silly. Just introduce another factor
x * x
to the previous term.
Worst of all is using a factorial function
: (a) factorials grow excessively fast; and (b) you would end up repeating a whole shed-load of multiplies that you had already done on previous terms. Just divide the previous term by the appropriate new factors.
For the sine series, just
(a) start with i=0, term=x, sum=term.
(b) loop in i, multiplying the previous term by
. Add this to the running sum.
using namespace std;
double Sine( double x )
const double tolerance = 1.0e-30;
double term = x, sum = term; // first term
double xsq = x * x; // convenience
for ( int i = 1; abs( term ) > tolerance; i++ )
term *= -xsq / ( 2 * i ) / ( 2 * i + 1); // next term as multiple of previous
sum += term; // add to sum
const double PI = 4.0 * atan( 1.0 );
cout << fixed << setprecision( 6 );
for ( int deg = 0; deg <= 90; deg += 10 )
double rad = deg * PI / 180.0;
cout << deg << '\t' << Sine( rad ) << '\t' << sin( rad ) << '\n';