What's wrong? How can I change the "sum1" order?
I'm trying to compute the value of cos x
using the Taylor series formula
infinity
---- 2k
\ k x
cos(x) = / (-1) * -------------
---- (2k)!
k=0
Shown graphically at http://ppt.cc/G,DC
Here is my program.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double sum=0.0,sum1=0.0;
double x;
cin>>x;
for(int i=0 ; i<=10 ; i=i+1 )
{
for(int i=1 ; i<=20 ; i=i+1)
{
sum1=i*sum1+sum1;
}
sum=pow(-1,(double)i)*pow(x,(double)(2*i))/sum1+sum;
}
cout<<"Sum : "<<sum<<endl;
system("pause");
return 0;
}
Th开发者_Go百科e output is -1.#IND
Why?
How can I change the order of "sum1" to make it work right?
You're using i
as the name of the controlling variables for two for-loops that are nested inside each other. That won't work the way you expect.
Next, sum1
is 0. No matter how many times you multiply zero by things and add zero to it, it's still zero. Then you divide by zero, which is why your final answer is NaN
(not-a-number).
You need to fix the computation of factorial. Why don't you write a factorial function and test it by itself first?
You're redeclaring i inside your inner loop.
for(int i=0 ; i<=10 ; i=i+1 )
{
for(int i=1 ; i<=20 ; i=i+1)
It's been a while since I've done C, but I'm fairly sure that's an error.
Many things are a bit weird. First : Please write ANSI C++ and try not to adopt the Microsoft Stuff, I don't really know but I guess those are for the pro's. Lets just stick to the basic stuff. Here is what you should do :
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
double factorial(double fac)
{
if(fac == 0)
return 1;
return fac * factorial(fac - 1);
}
int main(int argc, char* argv[])
{
double sum=0.0;
double x;
cin >> x;
for ( int i = 0 ; i <= 10 ; i++ )
{
double divisor = factorial ( 2 * i );
if(divisor != 0.0)
{
sum += (double)( (pow( -1 , i ) * pow (x , 2*i )) / divisor );
}
}
cout<<"Sum : "<<sum<<endl;
//system("pause");
return 0;
}
You are not only calculating the Factorial in a weird way, but you also dont use the math operators correctly and you dont perform the math calculation as you would like to. Also the code you wrote is very weird that way because it does not make it clear (not even for you from what I understand). Look at what others commented too. They are right.
When you divide by 0, the result becomes infinity (which prints out as -1.#IND
)
Muggen has given a good naive way of doing this, recomputing the whole factorial each time, and using the pow
function to compute the alternating sign in the formula. But there are improvements that you can make to this code faster.
The Factorial function in one iteration of the loop can take advantage of the fact that you already multiplied most of the terms you need in the prior iterations of the loop.
The exponent
(-1)^k
is just a way to alternate between addition and subtraction -- you can replace that by having a variable that alternates its sign every iteration through the loop. (There are other ways to do this besides what I showed here, the point is that you don't need to call thepow()
function to do it.)The other power function
x^(2k)
can also be unrolled the same way.I eliminated the first iteration of the loop, because I could calculate it in my head (it was 1.0, for any
x
), and set the initial value ofsum
to1.0
. This wayfactorial
doesn't ever get multiplied by 0.
Try this instead
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double x;
cin>>x;
double sum=1.0, factorial=1.0, sign=-1.0, power=1.0;
for(int i=1 ; i<=10 ; i=i+1 )
{
factorial*= (2*i-1) * 2*i;
power *= x * x;
sum += sign * power/factorial;
sign = -sign;
}
cout<<"Sum : "<<sum<<endl;
system("pause");
return 0;
}
It does not appear that you are computing the factorial correctly. should be
sum1 = 1.0;
for(int k=1 ; k<=i*2 ; k=k+1)
{
sum1 *= k;
}
Notice that the factorial terminates a at your outer loop i, and not the fixed number 20, When i is 5, you don't want 20!, you want (2*5)!.
精彩评论