inptTol = float(input("Enter the tolerance: "))
print()
term = 1
divNum = 3
npower = 1
sumPi = 0.0
count = 0
while abs(term) > inptTol:
sumPi += term
term = -term/(divNum * (3**npower))
divNum += 2
npower += 1
count += 1
sumPi = math.sqrt(12) * sumPi
pythonPi = math.pi
approxError = abs (sumPi - pythonPi)
print("The approximate value of pi is %.14e\n" \
" Python's value of pi is %.14e\n"
"The error in the approximation of pi is %.6e\n"
"The number of terms used to calculate the value of pi is %g " %
(sumPi, pythonPi, approxError, count))
以下是它显示的值:
pi的近似值为3.08770957930231e+00
Python的pi值是3.14159265358979e+00
我想让它告诉我:
pi的近似值为3.14159265358979
Python的pi值是3.14159265358979
我想你错过了
signal
。显然你试过这么做,但是改变了上一个学期的信号,并在下一个学期使用它。 看我的代码,我试着像他一样。你怎么认为?在您的
term
计算错误:假设}。此行不会将
term
当前是{term
设置为1/(5 * 3**2)
;它将term
设置为1/(3*3) / (5 * 3**2)
。你减少的term
比你想象的要多。在对我来说,问题是因为你改变了
term
值。它必须是1
或-1
-符号。在我的版本-我使用
for
循环7个学期的结果
^{pr2}$15个学期的结果
编辑:使用
while
循环的版本相关问题 更多 >
编程相关推荐