如何在 c++ 中使用 arctanx 函数修复此错误

How do I fix this bug with arctanx function in c++

本文关键字:函数 错误 arctanx c++      更新时间:2023-10-16

我最近被老师问了一个关于一个叫做arctanx公式的数学方程/公式的问题。问题是:

According to the Arctanx(x) = x - ((x ^ 3) / 3) + ((x ^ 5) / 5) - ((x ^ 
7) / 7) + ...and π = 6 * arctanx(1 / sqrt(3)), Create function arctanx(x)
, and find pi when the last "number"(like this ((x ^ y) / y)) is right before
 and bigger than 10 ^ -6, or you can say that no "number" can be smaller than
that number without being smaller than 10 ^ -6.

我试图将其编码出来,但其中有一个错误。

# include<iostream>
# include<math.h>
using namespace std;
float arctanx() {
    long double pi = 3.1415926535897;
    int i = 0; // 0 = +, 1 = -
    float sum = 0;
    float lsum;
    for (int y = 1; y < pi; y += 2) {
        if (lsum > 0.000001) {
            if (i == 0) {
                lsum = pow(1 / sqrt(3), y) / y;
                sum += pow(1 / sqrt(3), y) / y;
                i++;
            } else if (i == 1) {
                lsum = pow(1 / sqrt(3), y) / y;
                sum -= pow(1 / sqrt(3), y) / y;
                i--;
            }
        } else {
            break;
        }
    }
    sum = sum * 6;
    return sum;
}
int main() {
    cout << arctanx();
    return 0;
}

它应该有一个不等于零的数字的输出,但我从运行这个中得到了 0。

您的程序具有未定义的行为,因为您在比较if (lsum > 0.000001)中使用了未初始化的float lsum;。在您的情况下可能发生的情况是,lsum恰好小于或等于 0.000001,并且您的for立即break s,而无需执行任何操作导致您的函数返回0 * 6这显然是0

创建函数 arctanx(x(

发布的代码中定义的函数不接受任何参数,它只使用硬连线(和重复(值1 / sqrt(3)并尝试返回近似值 π 而不是 x 的反正切值。

它还具有未定义的行为,当它首次用于循环内的比较时,lsum未初始化(因此具有不确定的值(。

请考虑此实现,但请注意,对于大于 1 的 x 值,此特定极积分展开会发散。

#include <iostream>
#include <iomanip>
#include <cmath>
double arctanx(double x);
int main()
{
    double pi = 6.0 * arctanx(1.0 / std::sqrt(3));
    std::cout << std::setprecision(8) << pi << 'n';
}
double arctanx(double x)
{
    // You can take advantage of a running power, instad of calculating
    // pow(x, i) at every iteration
    double sq_x = x * x;
    double pow_x = x * sq_x;
    double err = 1e-6;
    // Instead of keeping track of the alternating sign, you can use
    // two separate partial sums
    double sum_pos_term = x;
    double sum_neg_term = 0.0;
    for (int i = 3; i < 33; i += 2)  // <- Limit the number of iterations
    {
        if (pow_x < err * i)
            break;
        sum_neg_term += pow_x / i;
        i += 2;
        pow_x *= sq_x;
        if (pow_x < err * i)
            break;
        sum_pos_term += pow_x / i;
        pow_x *= sq_x;
    }
    return sum_pos_term - sum_neg_term;
}