擅长:python、mysql、java
<p>返回表达式<code>n == Pi</code>始终是<code>False</code>,它在算术上是<code>0</code>。只需返回<code>Pi</code>。在</p>
<p>EDIT2:输出仍然为0的原因是Pi(0)-actualPi<;10**M表示任何非负M</p>
<p>还有其他问题,可以通过将Pi测试放在Pi函数的循环中来避免,如下所示。在</p>
<pre><code>actual_pi =3.1415926535897932384626433832795028841971693993751058209749445923078164062
def pi(testexp=14):
M = 1.0
denom = 2.0
Pi = 3.0
for i in range(50000):
delta = abs(Pi - actual_pi)
if not i % 1000: print(i, delta)
if delta <= (10 ** -14):
return i - 1 # Last iteration did not change Pi
else:
Pi += ((4.0/(denom*(denom+1)*(denom+2.0)))*M)
denom += 2.0
M *= -1.0
print(pi())
# 24834
</code></pre>