[英]C program to calculate the sine of x
This is what i have done so far. 这是我到目前为止所做的。 I don't know what is wrong with the code.
我不知道代码有什么问题。 In theory it should run perfectly well (or I might be wrong), but it just doesn't and it is driving me crazy.
从理论上讲,它应该运行得很好(或者我可能错了),但事实并非如此,这让我发疯。 I'm a beginner BTW.
我是初学者BTW。
Can anyone please point out what is wrong with the code? 谁能指出代码有什么问题吗?
# include <stdio.h>
# include <conio.h>
# include <math.h>
main()
{
int i , sum = 0 , n;
float x;
printf("Please enter the desired values for x and n (n>0): ");
scanf("%f %d",&x,&n);
for(i=1;i<=n;i++)
{
sum = sum +((pow(-1,i+1)*pow(x,2*i-1))/(factorial(2*i-1)));
}
printf("%f",sum);
}
int factorial(int n)
{
int c;
int result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}
Main issues: 主要问题:
sum
type should be float
or double
, not int
; sum
类型应该是float
或double
,而不是int
; factorial(int n)
must be able to return very big numbers, so its return type should be double
too. factorial(int n)
必须能够返回很大的数字,因此其返回类型也应该为double
。 Possible solution: 可能的解决方案:
# include <stdio.h>
# include <math.h>
double factorial(int n)
{
if (n == 0) return 1;
return n * factorial(n-1);
}
main()
{
int n;
double x, sum = 0;
printf("Please enter the desired values for x and n (n>0): ");
scanf("%lf %d", &x, &n);
for(int i = 0; i <= n; i++)
{
sum += pow(-1, i) * pow(x, 2 * i + 1) / (factorial(2 * i + 1));
}
printf("%f", sum);
}
To make your sine calculator bulletproof you should add some lines to check the value of the input x
and reduce it at least to the domain [-pi, pi] before evaluating the series. 为了使您的正弦计算器防弹,您应该添加一些行以检查输入
x
的值,并在评估该序列之前将其至少减小到域[-pi,pi] 。 Check out my answers here and here to understand why. 在这里和这里查看我的答案,以了解原因。
@Busy Beaver good answer points out some failings in OP code. @Busy Beaver好的答案指出了OP代码中的一些失败。
But to dig deeper on how OP could solve this without Stack Overflow . 但是要深入研究OP如何在没有堆栈溢出的情况下解决此问题。
Can anyone please point out what is wrong with the code?
谁能指出代码有什么问题吗?
Rather than seek someone to assist, use your compiler first. 而不是寻求帮助的人,请先使用编译器。 Enable all compiler warnings.
启用所有编译器警告。 A good compiler will complain about things like below.
一个好的编译器会抱怨类似下面的事情。 This is faster feedback than posting on SO.
这比在SO上发布反馈更快。
// return type defaults to 'int'
main()
// this should be as below (amongst other possibilities)
int main(void)
// implicit declaration of function 'factorial'
sum = sum +((pow(-1,i+1)*pow(x,2*i-1))/(factorial(2*i-1)));
// factorial should be declared/defined before it it used
// conversion to 'int' from 'double' may alter its value
sum = sum +((pow(-1,i+1)*pow(x,2*i-1))/(factorial(2*i-1)));
// This is the hint that `sum` should also be a floating point.
// format '%f' expects argument of type 'double', but argument 2 has type 'int'
printf("%f",sum);
// sum is type `int`, the matching specifier is "%d"`.
By fixing these warnings, code "works" without other changes. 通过解决这些警告,代码“工作”无需进行其他更改。 Still there are issues of precision, limited range, efficiency and overflow in the
factorial()
calculation. 在
factorial()
计算中仍然存在精度,范围有限,效率和溢出的问题。 Lesson to learn: use your compiler to help with the basic issues. 要学习的课程:使用编译器来解决基本问题。
Please enter the desired values for x and n (n>0): 1 5
0.841471
I now see this is an old post and OP may be have left the building . 我现在看到这是一个旧职位,OP可能已经离开了建筑物 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.