谁能告诉我为什么我的代码显示错误的 pi 值?

Can anyone tell me why is my code showing the wrong value of pi?

这是我的输出图片:

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

您正在使用 e 作为数字格式,这意味着:

Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent.

如果你想要定点输出你可以使用f:

Fixed point. Displays the number as a fixed-point number.

其他formats/options可以在documentation

中找到

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

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

Demo

至于我的问题是因为您更改了 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))

你的term计算有误:

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

假设 term 目前是 -1/(3*3)。此行不会将 term 设置为 1/(5 * 3**2);它会将 term 设置为 1/(3*3) / (5 * 3**2)。您减少的 term 比您预期的要多。

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

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

然后递归定义将产生适当的值。这个定义会翻转符号,去掉分母中旧的奇数,在分母中加上新的奇数,在分母中加上一个因数3。

您可以使用这种模式为近似值中的项生成分母。我会让你做除法和求和,最后乘以 sqrt(12)

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

我想你错过了 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))