微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在不使用外部库的情况下,使用 C++ 中的伯努利级数展开计算角度的余弦/正弦/正切

如何解决在不使用外部库的情况下,使用 C++ 中的伯努利级数展开计算角度的余弦/正弦/正切

我似乎无法找到错误。我需要一双新鲜的眼睛。我试图在不使用内置函数或库的情况下计算 C++ 中角度的 Cos/Sin/Tan 的值。这些是唯一的要求。

这是我目前得到的: 我编写了一个函数来计算指数值、阶乘和所需值。

我不断收到错误,我不知道它们的来源。有些数字给了我完全正确的答案,而其他数字则相差甚远。那么你能告诉我我在这里做错了什么吗?

#include <iostream>
#include <cmath>
using namespace std;

double bernoulli_numbers[] = { 1,-1/2.0,1/6.0,-1/30.0,5/66.0,-691/2730.0,7/6.0,-3617/510.0,43867/798.0,-174611/330.0,854513/138.0};

int angletoRadian(int angle) {
    float rad_angle;
    rad_angle = angle * (M_PI / 180);
    return rad_angle;
}

int calculatingExponents(int num,int power) {
    long double result = 1;
    for (int i = 0; i < power ; ++i) {
        result *= num;
    }
    return result;
}

unsigned long long calculatingFactorials(int n) {
    unsigned long long factorial = 1;
    if ( n < 0 ) {
        cout << "Can't compute factorials for negative numbers" << endl;
    }
    else if ( n < 2 ) {
        return 1;
    }
    else {
        for (int i = n; (i >= 2) ; i--) {
            factorial = factorial * i;
        }
    }
    return factorial;
}

void calculatingCos(double angle) {
    long double numerator;
    long double last_term;
    unsigned long long denominator;
    long double result;
    long double final_result = 0;
    for (int i = 0; i < 15; ++i) {
        numerator   = calculatingExponents(-1,i);
        denominator = calculatingFactorials(2 * i);
        last_term   = calculatingExponents(angle,(2 * i));
        result =  (numerator / denominator) * last_term;
        final_result += result;
    }
    cout << "The Cosine of the angle = " << final_result << endl;
}

void calculatingSin(double angle) {
    long double numerator;
    long double last_term;
    unsigned long long denominator;
    long double result;
    long double final_result = 0;
    for (int i = 0; i < 15; ++i) {
        numerator   = calculatingExponents(-1,i);
        denominator = calculatingFactorials((2 * i) + 1);
        last_term   = calculatingExponents(angle,((2 * i) + 1));
        result =  (numerator / denominator) * last_term;
        final_result += result;
    }
    cout << "The Sine of the angle = " << final_result << endl;
}

void calculatingTan(double angle) {
    int bernoulli_index;
    long double bernoulli_number;
    long double numerator;
    long double last_term;
    unsigned long long denominator;
    long double result;
    long double final_result = 0;
    for (int i = 0; i < 15; ++i) {
        bernoulli_index = (2 * i) + 2;
        bernoulli_number = bernoulli_numbers[bernoulli_index];
        numerator   =
        calculatingExponents(-1,i)
        *
        calculatingExponents(2,(2 * i) + 2)
        *
        ( calculatingExponents(2,(((2 * i) + 2) * 1)) - 1) * bernoulli_number;
        denominator = calculatingFactorials((2 * i) + 2);
        last_term   = calculatingExponents(angle,(2 * i) + 1);
        result =  (numerator / denominator) * last_term;
        final_result += result;
    }
    cout << "The Tan of the angle = " << final_result << endl;
}

int main() {
    int degree_angle;
    int x;
    cout << "Please input an angle in degrees:" << endl;
    cin >> degree_angle;
    x = angletoRadian(degree_angle);
    
    calculatingCos(x);
    calculatingSin(x);
    calculatingTan(x);
}

我遇到的一些错误是:

Input: 180
Output: 
The Cosine of the angle = -0.989992
The Sine of the angle = 0.14112
The Tan of the angle = 3.49908e+07
Expected Outputs:
The Cosine of the angle = -1
The Sine of the angle = 0
The Tan of the angle = 0

Input: 60
Output:
The Cosine of the angle = 0.540302
The Sine of the angle = 0.841471
The Tan of the angle = 1705.3
Expected Outputs:
The Cosine of the angle = 0.5
The Sine of the angle = 0.86602540378
The Tan of the angle = 1.73205080757

解决方法

许多参数和变量只有作为双精度值才有意义时都是 int

#include <iostream>
#include <cmath>
using namespace std;

double bernoulli_numbers[] = { 1,-1/2,1/6,-1/30,5/66,-691/2730,7/6,-3617/510,43867/798,-174611/330,854513/138};

double angleToRadian(int angle) {
    double rad_angle = angle * (M_PI / 180);
    return rad_angle;
}

angleToRadian 之前返回了 int,圆中有 pi 弧度,因此 360 度以下的角度将捕捉到 0、1、2 或 3 弧度。您需要返回一个小数才能获得正确的转换。

long double calculatingExponents(double num,int power) {
    long double result = 1;
    for (int i = 0; i < power ; ++i) {
        result *= num;
    }
    return result;
}

num 中的 calculatingExponents 参数取小数点作为 angleToRadians 的结果,所以 numresult 和 {{1} 的返回类型}} 都需要是浮点数。

calculatingExponents

如果您使用的是 unsigned long long calculatingFactorials(int n) { unsigned long long factorial = 1; if ( n < 0 ) { cout << "Can't compute factorials for negative numbers" << endl; } else if ( n < 2 ) { return 1; } else { for (int i = n; (i >= 2) ; i--) { factorial = factorial * i; } } return factorial; } 但返回的是 unsigned long longyou're relying on implementation defined behaviour,因此无法保证得到合理的答案。

int

再次 int main() { int degree_angle = 45; cout << "Angle in degrees: " << degree_angle << endl; double x = angleToRadian(degree_angle); calculatingCos(x); calculatingSin(x); calculatingTan(x); } 返回一个浮点数,所以 angleToRadian 需要是一个浮点类型。

这些修改似乎修正了 -90 到 90 度之间的正弦和余弦。

我还不确定切线计算中发生了什么错误。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。