C++ 代码实现麦克劳林公式求解 sin(x) 近似值 (误差 < 0.5e-13)
下面是使用麦克劳林公式求 sin(x) 的近似值,并确保截断误差小于 0.5 * 10^(-13) 的 C++ 代码:
#include <iostream>
#include <cmath>
double calculateSin(double x) {
double result = 0.0;
double term = x;
for (int n = 1; std::abs(term) > 0.5e-13; ++n) {
result += term;
term *= -x * x / ((2 * n) * (2 * n + 1));
}
return result;
}
int main() {
double x;
std::cout << "Enter the value of x in radians: ";
std::cin >> x;
double sinX = calculateSin(x);
std::cout << "Approximate value of sin(x): " << sinX << std::endl;
return 0;
}
请注意,这只是一种近似计算 sin(x) 的方法,并且在给定的截断误差要求下提供了一个近似值。由于麦克劳林级数近似不应用于非常大的 x 值,因此请在输入 x 值时确保其在合理的范围内。
原文地址: https://www.cveoy.top/t/topic/xVs 著作权归作者所有。请勿转载和采集!