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

2024-09-25 08:40:48 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: oftheisvaluecountpiabsprint
3条回答

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

您的term计算错误:

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

假设term当前是{}。此行不会将term设置为1/(5 * 3**2);它将term设置为1/(3*3) / (5 * 3**2)。你减少的term比你想象的要多。在

对我来说,问题是因为你改变了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个学期的结果

^{pr2}$

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))

相关问题 更多 >