用公式π/4≈1-1/3+1/5-1/7+……求π的近似值,直到发现某一项的绝对值小于10的-6次方为止,用C语言写出
时间: 2023-06-05 21:08:38 浏览: 247
DSP中的基于DSP的π/4-DQPSK调制快速位定时捕获算法的设计与实现
实现代码:
#include <stdio.h>
double pi_approximation() {
double pi = 0.0;
double divider = 1.0;
int sign = 1;
while (1) {
pi += sign / divider;
sign = -sign;
divider += 2.0;
if (1.0 / divider < 0.000001) {
break;
}
}
return pi * 4;
}
int main() {
printf("Approximation of pi using the formula: π/4 ≈ 1-1/3+1/5-1/7+... until absolute value less than 10^-6\n");
printf("π ≈ %lf\n", pi_approximation());
return 0;
}
阅读全文