根据泰勒级数 cosx = 10! - x^22! + x^44! - x^66! + 编写函数double myCosdouble求cosx的近似值。
#include <iostream>
#include <cmath>
using namespace std;
double myCos(double x) {
double ans = 1, term = 1;
for (int i = 1; i <= 10; i++) { // 取前10项进行近似
term *= -x * x / (2 * i - 1) / (2 * i);
ans += term;
}
return ans;
}
int main() {
double x;
cout << "请输入x的值:";
cin >> x;
cout << "cos(" << x << ") 的近似值为:" << myCos(x) << endl;
cout << "cos(" << x << ") 的真实值为:" << cos(x) << endl; // 对比真实值
return 0;
}
原文地址: http://www.cveoy.top/t/topic/hgT4 著作权归作者所有。请勿转载和采集!