我正在尝试将我的程序的结果扩展到小数点后 8 位。我浏览了这个页面(http://www.cplusplus.com/reference/ios/ios_base/precision/)但我没有任何运气改变结果。有什么建议么?谢谢。
#include <iostream>
#include <math.h>
using namespace std;
long double taylorSeriesToCalculateCosine(long double x, int degree);
long factorial( int input );
int main(int argc, const char * argv[])
{
cout << "Cosine of .3: ";
cout << taylorSeriesToCalculateCosine(.3,3) << endl;
return 0;
}
// taylor series to calculate cosine
long double taylorSeriesToCalculateCosine(long double x,int degree){
int i = 0;
long double accumulation = 0;
int cosInputArray[4] = {1,0,-1,0};
while (i < degree) {
int input = cosInputArray[i%4];
accumulation += input*((pow(x, i))/factorial(i));
i++;
}
return accumulation;
}
输出:
Cosine of .3: 0.955