我正在编写一个程序来计算“泰勒级数”,并且得到“inf”作为我的输出。我的程序还没有完成,一切都还没有正常工作,但我希望能够看到我的输出。有没有办法缩短输出或使其可见?谢谢您的帮助。
#include <iostream>
#include <math.h>
using namespace std;
long double taylorSeries(long double input, int degree);
long int factorial(int input);
long double derivative(long double input);
int main(int argc, const char * argv[])
{
long double taylorInput = cos(0);
cout << taylorSeries(taylorInput, 3) << endl;
return 0;
}
// taylor series function
long double taylorSeries(long double input, int degree){
long double i = 0;
long double accumulation = 0;
while (i < degree) {
derivative(input);
accumulation += (pow(input, i))/factorial(i);
i++;
}
return accumulation;
}
// function to calculate factorial
long int factorial(int input){
long int factorial = 0;
if (input == 1) {
factorial = 0;
}
while (input > 0) {
if (factorial == 0) {
factorial = input * (input - 1);
input -= 2;
}
else if (input > 0) {
factorial *= input;
input--;
}
}
return factorial;
}
long double derivative(long double input){
long double derivativeResult = 0;
derivativeResult = (((input + .001) - (input)) / .001);
return derivativeResult;
}