'Taylor Series'를 계산하는 프로그램을 작성 중이며 'inf'가 출력됩니다. 나는 프로그램으로 끝나지 않았고 모든 것이 제대로 작동하지는 않지만 내 결과물을보고 싶습니다. 출력을 줄이거 나 볼 수있게 할 수있는 방법이 있습니까? 도와 주셔서 감사합니다. 하나의 요인도 없다 0 : accumulation += (pow(input, i))/factorial(i);
에서출력을 줄이는 방법은 무엇입니까? 현재 '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;
}
나는 당신이 의미하는 것이 _shorten output_을 이해하는지 잘 모르겠습니다. 디버거에서 계산을 수행하거나 각 반복 후에 삽입 된 인쇄 명령을 사용하여 값을 무한대로 전환하는 시점을 보았습니까? – jogojapan
몇 가지 'cout'문을 넣지 않는 이유는 무엇입니까? 하나는'input'과'derivativeResult'를 나타내는 파생 함수이고, 하나는'계승 (factorial)'중 하나입니다. 결과가 너무 커지면 "무한대"에 도달 할 것입니다 (즉, long double에 저장할 수 없음) ... 당신은 당신이 해결해야만하는 문제에 내재되어 있는지 생각할 수 있습니다. (어떤 경우에 당신은 임의의 정밀도 라이브러리를 찾으러 포기할 수 있습니다. 그것에 관한 많은 stackoverflow 질문들이 있습니다.), 실수를했거나, 거대한 중간 값을 피하는 다른 방법으로 계산할 수 있습니다 .... –
증분 단계를 보려면 디버거를 사용하십시오. –