【问题标题】:Calculating cos(x) through Maclaurin series approximation using a factorial function and a cosine one使用阶乘函数和余弦函数通过 Maclaurin 级数逼近计算 cos(x)
【发布时间】:2018-06-21 04:32:55
【问题描述】:

我有一个任务是编写一个程序来通过Maclaurin approximation 计算 cos(x)。但是,我必须为 cos(x) 使用一个函数,并使用另一个函数来计算 cos(x) 函数内部分母上的指数。我认为大部分内容都是正确的,但我可能遗漏了一些东西,我不知道是什么。

#include<stdio.h>
#include <stdlib.h>
#include <math.h>
int fat(int);
float cosx(float);
int main()
{
    float x1;
    /* Original code: **x1 = x1 * 3.14159 / 180;** `transforms the value to radians` */
    x1 = x1 * 3.14159 / 180;                     /* transforms the value to radians */
    printf("Insert number:\n");
    scanf("%f", &x1);
    printf("Cosine of %f = %f", x1, cosx(x1));
    return 0;
}

int fat(int y)
{
    int n, fat = 1;
    for(n = 1; n <= y; n++)
    {
        fat = fat * n;
    }
    return fat;
}

float cosx(float x)
{
    int i=1, a = 2, b, c = 1, e;
    float cos;
    while(i < 20)
    {
        b = c * (pow(x,a)) / e;
        cos = 1 - b;
        a += 2;
        e = fat(a);
        c *= -1;
        i++;
    }
    return cos;
}

如果我输入 0,它会返回 -2147483648.000000,这显然是错误的。

【问题讨论】:

  • float x1; **x1 = ... 你到底想在这里做什么?
  • int fat(int y) 仅适用于 y &lt;= 12,因为 13! 太大而无法容纳 32 位数字。
  • @JGroven 这可能是x1 = x1 * 3.14159 / 180; /* transforms the value to radians */ 的格式错误。
  • 如果 @Yunnosch 是正确的,那么 OP 应该知道 scanf 正在覆盖 x1 的内容,除非用户以这种方式提供,否则不会将其表示为弧度。
  • 将未初始化的x1 值转换为弧度后,用scanf() 覆盖它(如果成功)。所以,虽然我想我知道你在那一行中做什么,但我想知道为什么你在那一行中这样做。

标签: c function


【解决方案1】:

第一个错误是未初始化变量x1,然后你就可以使用:

int x1; // <<< uninitiated variable;
**x1 = x1 * 3.14159 / 180;** `transforms the value to radians

这会产生随机值,你应该放

int x = 0; // or some other value of your choice

我认为你应该在scanf("%d", x1)之后移动x1 = x1 * 3.14159/100;

在使用前比再次初始化值e

int i=1, a = 2, b, c = 1, e;
...
b = c * (pow(x,a)) / e;
...

比你在b = c * pow(x,a) 行中的值可能超出int 变量的范围。如果e = 1x = 2a &gt; 31 超出b 的范围。另一个问题是pow(x,a) 的上升速度比`e 快得多。因此你得到越来越大的值,因此你得到另一个溢出。这是有效的代码:

#include <stdio.h>                                                                                                                                                                                                                       
#include <stdlib.h>
#include <math.h>

long double fact(int);
long double cosx(double);
long double my_pow (double b, int e);

int main()
{
    double x1 = 45.00;
    printf("Insert number:\n");
    scanf("%lf", &x1);
    x1 = x1 * 3.14159 / 180; // ** `transforms the value to radians`

    printf("Cosine of %f = %.10LF", x1, cosx(x1));
    return 0;
}

long double fact(int y)
{
    int n;
    double fact = 1;
    for(n = 1; n <= y; n++)
    {
        fact *= n;
    }
    return fact;
}

long double cosx(double x)
{
    int a = 2, c = -1;
    long i = 0, lim = 500;
    long double cos = 1;
    long double b = 0, e = 0;
    while(i < lim) {
        e = fact(a);
        b = c * my_pow(x,a);
        cos += b/e;
//      printf ("%le %le %le\n", e, b, cos);
        a += 2;
        c *= -1;
        i++;
    }

    return cos;
}

long double my_pow (double b, int e) {
    long double pow = 1;
    for (;e > 0; --e, pow *= b)
        ;
    return pow;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2018-10-06
    • 1970-01-01
    • 2019-07-11
    • 2017-11-26
    • 1970-01-01
    相关资源
    最近更新 更多