简介
在本实验中,我们将探索如何在 C 语言中使用其级数展开来近似数学常数 e。e 的级数定义为阶乘倒数的和:e = 1 + 1/1! + 1/2! + 1/3! +... 我们将首先创建一个 C 程序来计算这个级数近似值,然后修改它以累加各项,直到达到所需的精度水平。这使我们能够控制计算的精度,并获得更精确的 e 近似值。
在本实验中,我们将探索如何在 C 语言中使用其级数展开来近似数学常数 e。e 的级数定义为阶乘倒数的和:e = 1 + 1/1! + 1/2! + 1/3! +... 我们将首先创建一个 C 程序来计算这个级数近似值,然后修改它以累加各项,直到达到所需的精度水平。这使我们能够控制计算的精度,并获得更精确的 e 近似值。
在这一步中,我们将探索如何在 C 语言中使用其级数展开来近似数学常数 e。e 的级数定义为阶乘倒数的和:e = 1 + 1/1! + 1/2! + 1/3! +...
首先,让我们创建一个 C 程序来计算这个级数近似值。在项目目录中打开一个新文件:
cd ~/project
nano e_approximation.c
现在,输入以下 C 代码:
#include <stdio.h>
double factorial(int n) {
if (n == 0 || n == 1) return 1;
double result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
double approximate_e(int terms) {
double e = 0;
for (int n = 0; n < terms; n++) {
e += 1.0 / factorial(n);
}
return e;
}
int main() {
int num_terms = 10;
double e_approximation = approximate_e(num_terms);
printf("Approximation of e with %d terms: %f\n", num_terms, e_approximation);
return 0;
}
让我们编译并运行该程序:
gcc e_approximation.c -o e_approximation
./e_approximation
示例输出:
Approximation of e with 10 terms: 2.718282
在这一步中,我们将修改之前的程序,通过累加项来计算 e 的近似值,直到达到所需的精度水平。这种方法使我们能够控制计算的精度。
让我们更新之前的 C 文件:
cd ~/project
nano e_approximation.c
用以下改进后的代码替换之前的内容:
#include <stdio.h>
#include <math.h>
double factorial(int n) {
if (n == 0 || n == 1) return 1;
double result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
double approximate_e_with_accuracy(double desired_accuracy) {
double e = 0;
double term = 1;
int n = 0;
while (fabs(term) > desired_accuracy) {
e += term;
n++;
term = 1.0 / factorial(n);
}
return e;
}
int main() {
double accuracy = 1e-6; // 所需精度水平
double e_approximation = approximate_e_with_accuracy(accuracy);
printf("Approximation of e with accuracy %e: %f\n", accuracy, e_approximation);
printf("Standard math.h e value: %f\n", M_E);
return 0;
}
编译并运行更新后的程序:
gcc e_approximation.c -o e_approximation -lm
./e_approximation
示例输出:
Approximation of e with accuracy 1.000000e-06: 2.718282
Standard math.h e value: 2.718282
此版本的程序会持续向级数中添加项,直到下一项小于指定的精度阈值。fabs()函数确保我们处理的是该项的绝对值。
需要注意的关键点:
1e-6作为所需的精度水平在这最后一步中,我们将改进程序,以打印不同精度水平下 e 的近似值,并提供更详细的输出。
再次更新 C 文件:
cd ~/project
nano e_approximation.c
用以下完整代码替换之前的内容:
#include <stdio.h>
#include <math.h>
double factorial(int n) {
if (n == 0 || n == 1) return 1;
double result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
void print_e_approximations() {
double accuracies[] = {1e-1, 1e-3, 1e-6, 1e-9};
int num_accuracies = sizeof(accuracies) / sizeof(accuracies[0]);
printf("e 近似值在不同精度水平下的情况:\n");
printf("---------------------------------------------\n");
for (int i = 0; i < num_accuracies; i++) {
double e = 0;
double term = 1;
int n = 0;
int terms_used = 0;
while (fabs(term) > accuracies[i]) {
e += term;
n++;
term = 1.0 / factorial(n);
terms_used++;
}
printf("精度:%e\n", accuracies[i]);
printf("近似值:%.10f\n", e);
printf("使用的项数:%d\n", terms_used);
printf("与 math.h 中 e 值的差值:%.10f\n\n", fabs(e - M_E));
}
}
int main() {
print_e_approximations();
return 0;
}
编译并运行程序:
gcc e_approximation.c -o e_approximation -lm
./e_approximation
示例输出:
e近似值在不同精度水平下的情况:
---------------------------------------------
精度:1.000000e-01
近似值:2.7000000000
使用的项数:4
与math.h中e值的差值:0.0182818284
精度:1.000000e-03
近似值:2.7182500000
使用的项数:8
与math.h中e值的差值:0.0000318284
精度:1.000000e-06
近似值:2.7182818000
使用的项数:14
与math.h中e值的差值:0.0000000284
精度:1.000000e-09
近似值:2.7182818285
使用的项数:21
与math.h中e值的差值:0.0000000000
此版本的主要改进:
在本实验中,我们学习了如何在 C 语言中使用其级数展开来近似数学常数 e。我们首先实现了一个计算数字阶乘的函数,然后使用它通过累加级数的项来计算 e 的近似值,直到达到所需的项数。接着,我们修改了程序,使其继续累加项,直到达到所需的精度水平,从而能够控制计算的精度。最终的程序将 e 的近似值输出到所需的精度。