#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;
}
C++ 使用泰勒级数计算 cos(x) 近似值 - myCos() 函数实现

原文地址: https://www.cveoy.top/t/topic/oJQx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录