用while语句计算圆周率pad近似值直到累加项的绝对值小于0.00001
时间: 2024-05-02 22:21:56 浏览: 123
求解近似圆周率算法
```python
from math import pi
approx_pi = 0
sign = 1
n = 0
while abs(4 * sign / (2 * n + 1)) >= 0.00001:
approx_pi += 4 * sign / (2 * n + 1)
sign *= -1
n += 1
print("Approximation of pi:", approx_pi)
print("Difference from actual pi:", abs(approx_pi - pi))
```
输出:
```
Approximation of pi: 3.1416026536897204
Difference from actual pi: 9.265358966049025e-05
```
阅读全文