计算余弦算法

发布于 2024-12-27 21:57:35 字数 1630 浏览 2 评论 0原文

我创建了这个函数CalculateCos

int Factorial (long int n)
{
    long int r = 1;
    for (int i = 2; i<=n; i++)
    {
        r = r*i;    
    }   

    return r;
}

float CalculateVariable(int CVnumber, int CVloopCounter)
{
    float CVresult = 0;
    CVresult = pow(CVnumber, (CVloopCounter*2)) / (long int)Factorial(CVnumber*2);

    return CVresult;
}

float CalculateCos(int number)
{
    float result = 1;
    int loopCounter = 1;
    int minusOrPlus = 1;
    while(loopCounter <= precision && loopCounter <= 8)
    {
        if(!minusOrPlus)
        {
            result = result - CalculateVariable(number, loopCounter);
            printf("%f\n", result);
            minusOrPlus = 1;
        }
        else
        {
            result = result + CalculateVariable(number, loopCounter);
            printf("%f\n", result);
            minusOrPlus = 0;
        }
        loopCounter++;
      }
      return result;
}

我在减法或加法之后 printf 的原因是因为它给了我奇怪的输出,例如:

Enter a number, for the cos function
6
1.000000
0.999997
1.000095
0.996588
1.122822
-3.421593
160.177368
-5729.385254
Result is: -5729.3852539
Official function result is:  0.9601703

你能帮我得到正确的结果吗?

更新:

现在我的解决方案是:

float CalculateCos(float number)
{
    float result = 0;
    float step = 1;
    int loopCounter = 1;

    while(loopCounter <= 5)
    {
        step = step * (-number) * number / (((2*loopCounter)-1)*((2*loopCounter)-2));
        result += step;
        loopCounter++;
    }

    return result;
}

I created this function CalculateCos:

int Factorial (long int n)
{
    long int r = 1;
    for (int i = 2; i<=n; i++)
    {
        r = r*i;    
    }   

    return r;
}

float CalculateVariable(int CVnumber, int CVloopCounter)
{
    float CVresult = 0;
    CVresult = pow(CVnumber, (CVloopCounter*2)) / (long int)Factorial(CVnumber*2);

    return CVresult;
}

float CalculateCos(int number)
{
    float result = 1;
    int loopCounter = 1;
    int minusOrPlus = 1;
    while(loopCounter <= precision && loopCounter <= 8)
    {
        if(!minusOrPlus)
        {
            result = result - CalculateVariable(number, loopCounter);
            printf("%f\n", result);
            minusOrPlus = 1;
        }
        else
        {
            result = result + CalculateVariable(number, loopCounter);
            printf("%f\n", result);
            minusOrPlus = 0;
        }
        loopCounter++;
      }
      return result;
}

The reason why I printf after the subtraction or adding, is because it gives me strange output, like:

Enter a number, for the cos function
6
1.000000
0.999997
1.000095
0.996588
1.122822
-3.421593
160.177368
-5729.385254
Result is: -5729.3852539
Official function result is:  0.9601703

Can you help me to get correct results on this?

UPDATE:

Now my solution is:

float CalculateCos(float number)
{
    float result = 0;
    float step = 1;
    int loopCounter = 1;

    while(loopCounter <= 5)
    {
        step = step * (-number) * number / (((2*loopCounter)-1)*((2*loopCounter)-2));
        result += step;
        loopCounter++;
    }

    return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第七度阳光i 2025-01-03 21:57:35

当前问题:

由于您的 Factorial 函数返回 int 并且将其转换为 long int ,因此即使在输入之前,其结果也会溢出16 在你的情况下(14!> max_int)。

您正在使用泰勒级数计算 cos

cos(x) = 1 - x2/2! + x4/4! - x6/6!
+ ...

我不会写代码。但是您的程序中存在一些错误,可以轻松修复:

  1. 输入是弧度,因此 number 应该是 float
  2. 分别使用指数和阶乘计算泰勒级数的每一步很快就会导致溢出。正确的方法是维护一个 float 变量:step = 1 首先,在第 k 次循环迭代中 step = step * (- x) * x / ((2*k-1)*(2*k))。这样,您只需在循环中将 step 添加到 result 中,就不再需要 minusOrPlus 了。
  3. 循环迭代次数以 8 为界,该值太小,因此结果可能不够精确。
  4. 我没有看到您在任何地方使用精度变量。它可用于检查结果的精度。例如,当abs(step) abs(step) abs(step) abs(step) abs(step) abs(step) precision,我们将终止循环。

Current problem:

since your Factorial function returns int and you casts it to long int, its result is going to overflow even before the input goes to 16 in your case (14! > max_int).

You're calculating cos using Taylor series:

cos(x) = 1 - x2/2! + x4/4! - x6/6!
+ ...

I'm not going to write code. But there are some things wrong in your program, which can be fixed easily:

  1. The input is in radian, so number should be a float.
  2. Calculating each step of Taylor series using exponentiation and factorial separately leads to overflow very soon. The correct way is maintaining a float variable: step = 1 at first and in kth loop iteration step = step * (- x) * x / ((2*k-1)*(2*k)). In this way, you simply add step to result in the loop and don't need minusOrPlus anymore.
  3. The number of loop iterations is bounded by 8 which is too small, so the result could be not precise enough.
  4. I don't see you use precision variable anywhere. It could be used to check precision of the result. For example, when abs(step) < precision, we're going to terminate the loop.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文