-2

I am trying to calculate the value of sin(x) without using the predefined function. Instead I am trying to calculate it using Taylor series.

The issue is that the program produces correct values for small values of x(lesser than 10 to be precise).

I have included 1000 iterations of the series but still the program produces wrong answers for larger values of x.

float e,x,p=2;
int a;
float sum1;

cout<<"enter the value x for sinx"<<endl;
cin>>x;
e=x;
sum1=x;

for(a=1;a<1000;a++)
{   
    x=x*(e/p)*(e/(p+1))*(-1);   //MULTIPLYING PREVIOUS TERM to -1*e/p * e/p+1 to get the next term.
    sum1=sum1+x;
    p=p+2;
}
cout<<sum1<<endl;

return 0;

For large values of x (example x=100) , I am getting the NaN(not a number error) which is fine by me. But the issue lies is the fact that I'm getting results like sin(25)=278.2 even though i included 1000 iterations of Taylor series.

Here is the sample output.

enter image description here

PLEASE HELP!! , I'm curious to know what went wrong.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mujtaba1747
  • 33
  • 1
  • 6
  • 1
    Stop witing ALL-CAPS please, this is considered yelling and rude. – πάντα ῥεῖ Oct 27 '18 at 12:51
  • Have you had the curiosity to look at the values of the individual terms ? –  Oct 27 '18 at 12:58
  • 1
    That is normal for a Taylor approximation, the more the argument is away from 0, the more terms you need to get enough accuracy. Which is limited by itself due to the limited precision of the floating point representation. NaN if you just keep going blindly. A simple workaround is to normalize the argument, sin() is periodic between -pi/2 and pi/2 so larger values can be normalized with fmod(). – Hans Passant Oct 27 '18 at 12:58
  • @HansPassant: From −π/2 to π/2 is only half the period of sine, of course. I expect what you actually mean is sin(*x*) can always be reduced to sin(*y*) for some *y* in −π/2 ≤ *y* ≤ π/2. But that requires more than just `fmod`; you have to fold as well. (And `fmod` only works for “medium” values. It is exact, but only for the arguments it is given, and you cannot give it π.) – Eric Postpischil Oct 27 '18 at 13:38

1 Answers1

0

While the Taylor series for sin(x) converges (because eventually the factorial in the denominator grows larger than the exponential in the numerator), for modest values of x, the values involved become very large before the quotients become very small. In every floating-point multiplication, division, addition, and subtraction, there are rounding errors. The rounding errors are roughly proportional to the values involved. (In the format commonly used for float, they average more than 2−24 times the magnitude of the result.)

For 1000 terms, the x in your loop grows to at least 5.726•109, so the error in that term may be around 5.726•109•2−24 ≈ 341. So a final result of 278.2 is not unexpected.

If you change to double, the errors will be smaller (around 2−53 instead of 2−24), and you will get better results for input values around 25. Nonetheless, with larger input values, the errors will grow again.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312