[英]C - Sum of 1/n numbers
Hello could you take a quick glance at my code and point out the mistake(s). 您好,您可以快速浏览一下我的代码并指出错误。 I'm trying to calculate sum of n numbers going like this: 1- 1/2 + 1/3 - 1/4 ... etc...
我正在尝试计算这样的n个数字的总和:1- 1/2 + 1/3-1/4 ...等等...
With the following code, I get 1.00000 each time, but it should be between 0 and 1, for example for 3 it should be 1 - 1/2 + 1/3 = 0,83333. 使用以下代码,我每次得到1.00000,但是它应该在0和1之间,例如对于3,它应该是1-1/2 + 1/3 = 0,83333。
#include <stdio.h>
int main () {
int n, prefix;
float sum;
scanf("%d", &n);
do {
if (n%2==0) {
prefix=-1;
} else {
prefix=1;
}
sum+= prefix/n;
n = n - 1;
} while (n > 0);
printf("%f", sum);
}
Three errors that I can see: 我可以看到三个错误:
premix
vs prefix
) premix
vs prefix
) sum
sum
Try the following instead: 请尝试以下操作:
#include <stdio.h>
int main () {
int n, n_initial;
double sum=0.0, prefix = 1.0;
printf("enter the value for n:\n");
scanf("%d", &n);
if(n<1) {
printf("n must be > 0!\n");
return 1;
}
n_initial = n;
if (n%2==0) prefix = -1.0; else prefix= 1.0;
do {
sum+= prefix/(double)n;
prefix *= -1.0;
n--;
} while (n > 0);
printf("The sum of the series over %d terms is: %lf\n", n_initial, sum);
return 0;
}
Note - I keep n
as an integer, and cast it explicitly before the divition. 注意-我将
n
保留为整数,并在除法之前将其显式转换。 It might be better just to make it a float / double - remember to change the format specification for the scanf
accordingly. 最好将其设置为float / double-记住相应地更改
scanf
的格式规范。 I only do the modulo operation once (after that, the sign of prefix
just keeps changing). 我只执行一次模运算(此后,
prefix
的符号不断变化)。 Also - it's always a good idea to add a prompt for the input of a number ("why isn't it doing anything?!"), to annotate the result (rather than print "just a number"), and to end the output with a newline (so the prompt doesn't obscure the output of the program). 另外-总是建议输入数字(“为什么不做任何事情?!”),注释结果(而不是打印“仅数字”),并结束输入,这总是一个好主意。用换行符输出(因此提示不会混淆程序的输出)。
Finally - you might want to check that the user doesn't enter a negative number, which would make your code give a bad result. 最后-您可能要检查用户是否未输入负数,否则将使您的代码给出不好的结果。
As an afterthought - you could test for large values of n
, and just return log(2.0)
. 事后考虑-您可以测试
n
大值,然后返回log(2.0)
。 But that would be cheating... and this series does converge awfully slowly (it oscillates quite badly - the 3rd digit is still changing when n=1000). 但是,这将是在欺骗......而这一系列不收敛非常慢(它振荡相当糟糕-的第三个数字还在不断变化,当n = 1000)。 Consequently, rounding errors risk really compounding.
因此,四舍五入的错误确实有增加风险的风险。 This is why you need to be using the
double
type; 这就是为什么您需要使用
double
类型的原因。 but I would suggest it's instructive to look at other ways to compute log(2.0)
- for example, using one of the other series given at http://www.math.com/tables/expansion/log.htm . 但我建议您考虑一下其他计算
log(2.0)
,例如,使用http://www.math.com/tables/expansion/log.htm上提供的其他系列之一。 You could actually implement all of them, and compare their accuracy after n terms (by printing out the error: sum - log(2.0)
.) 您实际上可以实现所有这些函数,并在n个条件后比较它们的精度(通过打印错误:
sum - log(2.0)
。
在上面的答案中加上未初始化的总和 (将其初始化为零 )。并且我认为您的代码在输入为0时不起作用(它给出除以零的误差) 。因此,最好在while或for循环中使用或提出一个在做一会儿。
you are doing prefix/n
when both are integers. 当两者都是整数时,您正在做
prefix/n
。 so you are getting the answer as integer (which always is 0) 所以您得到的答案是整数(始终为0)
use 采用
(double)prefix/n
or 要么
prefix/(double)n
or even 甚至
(double)prefix/(double)n
here is the full code: 这是完整的代码:
#include <stdio.h>
int main () {
int n, prefix;
float sum = 0;
scanf("%d", &n);
do {
if (n%2==0) {
prefix=-1;
} else {
prefix=1;
}
sum+= (double)prefix/(double)n;
n = n - 1;
} while (n > 0);
printf("%f", sum);
}
i ran it on compileonline.com with the input of 10 and got: 我在compileonline.com上以10的输入运行它,并得到:
Both prefix
and n
are integers, so that means you will have integer division which truncates the result (ie simply cuts of the decimals). prefix
和n
都是整数,因此这意味着您将得到整数除法,该结果将被截断(即简单地切掉小数点)。 If you want a floating point result, one or both of the variables also have to be a floating point variable. 如果要浮点结果,则一个或两个变量也必须是浮点变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.