为什么下面的方法生成 2*sqrt(2) 而不是 pi?
我尝试沿着 90 度的圆形路径在 x 或 y 方向上迭代一步。完成后,我可以通过除以半径来计算 pi。但我一直得到 2.8 而不是 3.14。
代码:
import math
circ=1
res=1000000
x=res
y=0
while x>=y:
circ=circ+1
ops=[]
ops.append(abs(res**2 - (x-1)**2 - (y+0)**2))
ops.append(abs(res**2 - (x-0)**2 - (y+1)**2))
ops.append(abs(res**2 - (x-1)**2 - (y+1)**2))
ops.append(abs(res**2 - (x+1)**2 - (y+1)**2))
ops.append(abs(res**2 - (x-1)**2 - (y-1)**2))
o= ops.index(min(ops))
#print(o,ops)
if o==0: x=x-1
if o==1: y=y+1
if o==2: x=x-1; y=y+1
if o==3: x=x+1; y=y+1
if o==4: x=x-1; y=y-1
print(x,y)
print()
print(circ*4/res)
输出
707106 707108
2.828436
[Program finished]
即使对于对角线步骤,你也总是在
circ
上加 1。