c++ - Sin 算法不起作用

标签 c++ math visual-c++ trigonometry

我制作了一个不用 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 函数,只需进行最少的更改即可使其正常工作。 你做错了什么

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

  1. sin 在被修改之前没有被初始化
  2. if block 中正确标记 taylor 项的属性不正确。
  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;
    

关于c++ - Sin 算法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49908764/

相关文章:

c++ - 如何使用 Visual Studio 2010 从网络共享构建项目?

谁能把这个翻译成Python?

c++ - 快速计算 3 维空间中的粒子接近度

javascript - 如何测试一个点是否是二次贝塞尔曲线的一部分?

c++ - 是否可以在保持 RTTI 启用的情况下从可执行文件中删除类型名称?

visual-c++ - 如何检索 Windows 8 中 TSF 管理器使用的文档中的总字符数?

c# - 如何获取 Windows 应用商店应用程序的本地化显示名称

c++ - 模板别名上下文中的模板<>模板<>语法是什么?

c++ - 如何让一个 pthread 在另一个线程正在等待 C++ 中的信号量时继续?

c++ - 交互式编辑 QPixmap 上的现有矩形?