Sin 算法不起作用

Sin algorithm not working

我做了一个c++程序,不用math.h就可以计算sin。我将此算法用于我的程序 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 函数,只需稍作改动即可使其正常工作。 你做错了什么

这些只是最小的编辑,执行这些编辑后自然会进行一些代码风格清理。例如:ifelse 块(不是 else if)具有几乎完全相同的代码

  1. sin 在被修改之前没有被初始化
  2. if 块中纠正泰勒术语的归因不正确。
  3. 不需要在末尾从 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;