我写了这段代码。我认为这没关系,但是当我运行它时,我得到了一个糟糕的结果。此代码用于计算欧拉数。我会很感激你的回答。
我期望的结果大约是 2.718281828459045,我得到的结果是 2.718281745910644:
- 2.718281828459045(预期)
- 2.718281745910644(实际)
代码:
#include <stdio.h>
main() {
int factor, counter, n = 1;
float total = 0, division;
while ( n <= 20 ) {
counter = 1;
factor = n;
while ( counter < n ) {
factor *= ( n - counter );
counter++;
}
division = 1.0 / factor;
total = total + division;
n++;
}
total = total + 1;
printf( "La constante matematica e vale aproximadamente: %.20f\n", total);
return 0;
} /* Finaliza funcion main */