C计算正弦的C程序给出了不准确的结果



我必须仅使用#include<stdio.h>编写此程序。

我必须从用户读取系列'n'的最高功能。

x=45 and n=9时,该程序会给我0.7068251967。但是当我使用计算器相同时,我会得到0.7068251828

我还必须使用递归。

#include<stdio.h>
float pow(float n, int p)
{
if(p == 0)
    return 1;
else
    return n * pow(n, p-1);
}
int fact(int n)
{
if(n == 0)
    return 1;
else
    return n * fact(n-1);
}
int main()
{
int n, x, i, sign = 1;
float sum, r;

printf("Enter the angle in degrees.n");
scanf("%d", &x);
r = 3.14 * x / 180.0;
printf("Enter the odd number till which you want the series.n");
scanf("%d", &n);
if(n % 2 == 0)
    printf("The number needs to be an odd number.n");
else
{

for(i = 1, sum = 0; i <= n; i += 2, sign *= -1)
{
    sum += (sign * pow(r, i)) /  fact(i);
}
printf("The sum of the series is %.10f.n", sum);
}

return 0;
}

我认为一个原因是您将PI近3.14近似。也许是您的计算器考虑到更多的PI数字。尝试使用更多数字进行大约pi。

@mat是正确的,请使用m_pi代替您的'Poor Man'3.14。此外,并不总是将X指向功率n或阶乘。请注意,总和的下一个术语:a_ {k 2} = - a_ {k} x^2/(k(k-1)),(偶数为零)使用

之类的东西
double s=x,a=x,x2=-x*x;
int i;
for (i=3;i<=n;i+=2)
{
   a*=x2/i/(i-1);
   s+=a;
}

最新更新