我制作了一个不用 math.h 计算 sin 的 c++ 程序。我将此算法用于我的程序 https://ibb.co/bTnQnS .我输入 45 度,程序将度数转换为弧度,程序使用算法,程序输出 -0.868597。该程序应输出 0.70710678 或 √2/2。我在算法上做错了什么?
代码:
#include "stdafx.h"
#include <iostream>
using namespace std;
double sin(int input, int decimal_count);
int factorial(int n);
double deg_to_rad(int deg);
double power(double base, int power);
int main(){
double angle;
int decimal;
cout << sin(45,8) << endl;
//end
system("pause");
return 0;
}
double sin(int input, int accuracy) {
int odds = 3;
double sin;
double rads = deg_to_rad(input);
for (int i = 1; i <= accuracy; i += 1) {
if (i==1) {
sin = power(rads, odds) / factorial(odds);
}
else if (i%2==0) {
sin = (power(rads, odds) / factorial(odds)) + sin;
}
else {
sin = (power(rads, odds) / factorial(odds)) - sin;
}
odds = odds + 2;
}
sin = sin - rads;
return sin;
}
int factorial(int n) {
int fact = 1;
for (int j = 1; j <= n; j+=1) {
fact = fact * j;
}
return fact;
}
double deg_to_rad(int deg) {
return deg*(3.14159265/180);
}
double power(double base, int power) {
double ans = 1;
for (int k = 1; k <= power; k+=1) {
ans = ans * base;
}
return ans;
}
最佳答案
您的泰勒级数展开函数不正确。 :)
您必须忽略所有偶数项。
我已经为您修复了它(我删除了一些 Windows 特定的东西,因为我没有 Windows 机器:stdfax.h
header 和对 pause
的调用被删除)
# include <cstdlib>
# include <iostream>
using namespace std;
double sin(int input, int decimal_count);
int factorial(int n);
double deg_to_rad(int deg);
double power(double base, int power);
int main(){
double angle;
int decimal;
cout << "The sine value is: " << sin(45,8) << endl;
//end
system("sleep 2");
return 0;
}
double sin(int input, int accuracy) {
int odds = 3;
double sin;
double rads = deg_to_rad(input);
bool negative_flag = true;
cout << "You entered " << input << " degrees" << endl;
cout << "This is " << rads << " radians" << endl;
sin = rads;
for (int taylor_term = 3; taylor_term <= 7; taylor_term += 2) {
double term = (double)(power(rads, taylor_term) / factorial(taylor_term));
if (negative_flag) {
term = -1 * term;
}
negative_flag = !(negative_flag);
sin += term;
}
return sin;
}
int factorial(int n) {
int fact = 1;
for (int j = 1; j <= n; j+=1) {
fact = fact * j;
}
return fact;
}
运行这个输出
You entered 45 degrees
This is 0.785398 radians
The sine value is: 0.707106
说明
正弦的泰勒级数展开式是一系列具有奇数泰勒系数且符号交替的项。在我的代码中,交替符号受标志影响。我还只考虑了泰勒级数展开式的前 3 项。
除此之外,行 double term = (double)(power(rads, taylor_term)/factorial(taylor_term));
计算泰勒级数展开中的每一项。
negative_flag = !(negative_flag);
重置下一个学期的标志。
解决您的评论以及您的代码哪里有问题
下面是您的 sin 函数,只需进行最少的更改即可使其正常工作。 你做错了什么
这些只是最小的编辑,执行这些编辑之后自然会进行一些代码样式清理。例如:if
和 else
block (不是 else if
)具有几乎完全相同的代码
- sin 在被修改之前没有被初始化
- 在
if
block 中正确标记 taylor 项的属性不正确。 不需要在
sin
的末尾额外减去rads
。一旦这些问题得到解决,您的代码就可以工作了:)int odds = 3; double sin ; double rads = deg_to_rad(input); sin = rads; for (int i = 1; i <= accuracy; i += 1) { if (i==1) { sin = sin - power(rads, odds) / factorial(odds); } else if (i%2==0) { sin = (power(rads, odds) / factorial(odds)) + sin; } else { sin = -(power(rads, odds) / factorial(odds)) + sin; } odds = odds + 2; } return sin;
关于c++ - Sin 算法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49908764/