任何人都可以告诉我为什么我的代码显示错误的pi值?

时间:2016-01-27 22:59:04

标签: python pi

This is the equation i'm trying to use in the while loop以下是我的输出图片:

enter image description here

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

6 个答案:

答案 0 :(得分:1)

我认为你错过了signal。显然你试图这样做,但改变前一个术语信号并在下一个学期使用它。 看到我的代码,我试着像他一样。你觉得怎么样?

import math
inptTol = float(input("The tolerance: "))

signal = 1.0
term = 1.0
divNum = 3.0
npower = 1.0
sumPi = 0.0
count = 0.0

while inptTol < abs(term):
    signal *= -1.0
    sumPi += term
    term = signal / (divNum * (3.0 ** npower))
    divNum += 2.0
    npower += 1.0
    count += 1.0

sumPi *= math.sqrt(12.0)
pythonPi = math.pi  
approxError = abs(sumPi - pythonPi)  

print("The approximate value of pi is %.14f\n" \
        "       Python's value of pi is %.14f\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))

答案 1 :(得分:0)

至于我的问题是因为你更改了term值。必须是1-1 - 签名。

我的版本 - 我使用for循环

import math

terms_number = float(input("Enter terms number: "))

sign = 1
divNum = 1
npower = 0
sumPi = 0.0
count = 0

for x in range(terms_number):

    sumPi += sign/(divNum * (3**npower))

    # values for next term
    sign = -sign
    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))

7个学期的结果

The approximate value of pi is 3.14167431269884e+00
       Python's value of pi is 3.14159265358979e+00
The error in the approximation of pi is 8.165911e-05
The number of terms used to calculate the value of pi is 7 

15个学期的结果

The approximate value of pi is 3.14159265952171e+00
       Python's value of pi is 3.14159265358979e+00
The error in the approximation of pi is 5.931921e-09
The number of terms used to calculate the value of pi is 15
使用while循环

编辑版本

import math

inptTol = float(input("Enter the tolerance: "))
term = 1

sign = 1
divNum = 1
npower = 0
sumPi = 0.0
count = 0

while abs(term) > inptTol:

    term = sign/(divNum * (3**npower))

    sumPi += term

    # values for next term
    sign = -sign
    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))

答案 2 :(得分:0)

您的term计算错误:

term = -term/(divNum * (3**npower))

term目前是-1/(3*3)。此行不会将term设置为1/(5 * 3**2);它会将term设置为1/(3*3) / (5 * 3**2)。你比你应该更多地减少term方式。

答案 3 :(得分:0)

您似乎试图根据term[i+1]来定义term[i]。如果您将该行更改为:

term = -term*(divNum-2)/(divNum * 3)

然后递归定义将产生适当的值。这个定义将翻转符号,删除分母中的旧奇数,在分母中添加新的奇数,并在分母中添加因子3。

答案 4 :(得分:0)

您可以使用此类模式为近似项生成分母。我会让你进行除法和求和,最后乘以sqrt(12)

print [(-1)**(i%2)*(3**(i)*(1+i*2)) for i in range(0,10)]

答案 5 :(得分:-1)

您使用e作为数字格式,表示:

  

指数表示法。使用字母“e”以科学记数法打印数字以表示指数。

如果您想要定点输出,可以使用f

  

定点。将数字显示为定点数。

其他格式/选项可在documentation

中找到

在您的代码中发现了错误。每个term都是使用之前的term作为分子来计算的,而实际上你只需要交替-1和1。更改计算term的公式可解决问题:

term = ((-1)**npower)/(divNum * (3**npower))

Demo