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

标签 c recursion trigonometry

我只能用 #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。

关于计算正弦的 C 程序给出的结果不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55302837/

相关文章:

c++ - CS高级项目思路涉及Unix系统编程

powershell - 递归添加文件到文件夹,如果不存在则创建文件夹

java - Java中的正弦近似误差

c++ - 圆上两个度数标记之间的最短距离?

matlab - 我的余弦不接受 matlab 中的符号变量

objective-c - 在 obj-c 对象而不是 C 结构上调用 IOUSBDeviceInterface 函数

c - Linux C 套接字 - 保持服务器处于事件状态

c++ - 使用在不同源文件中定义的变量

php - 当参数的数据结构变化时递归函数?

java - JPA递归延迟加载失败