我编写了一个c ++程序,该程序无需math.h即可计算正弦。我在程序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;
}

最佳答案

您的taylor系列展开功能不正确。 :)

您必须忽略所有偶数条件。

我已经为您修复了它(我删除了一些Windows特定的东西;没有Windows机器:stdfax.h标头和对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


说明

正弦的泰勒级数展开是奇数泰勒系数在符号上交替的一系列项。在我的代码中,交替符号受标志影响。我也只考虑了taylor系列扩展的前三个学期。

除此之外,行double term = (double)(power(rads, taylor_term) / factorial(taylor_term));计算taylor级数展开中的每个项。

negative_flag = !(negative_flag);重置下一项的标志符号。

解决您的评论以及代码有误的地方

以下是您的罪恶功能,只需进行最小的更改即可使其工作。
你做错了什么

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


在修改之前未初始化sin
if块中的taylor术语的正确符号归属不正确。
不需要在rads末尾加上sin的额外减法。这些问题解决之后,您的代码就可以工作了:)

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;

09-10 03:23