加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 13:09:47 所属栏目:Python 来源:网络整理
导读:这是我输出的图片: inptTol = float(input("Enter the tolerance: "))print()term = 1divNum = 3npower = 1sumPi = 0.0count = 0while abs(term) inptTol: sumPi += term term = -term/(divNum * (3**npower)) divNum += 2 npower += 1 count += 1sumPi = m

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 %.14en" 
        "       Python's value of pi is %.14en"
        "The error in the approximation of pi is %.6en"
        "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

解决方法

至于我的问题是因为你改变了术语价值.它必须是1或-1 – 符号.

我的版本 – 我用于循环

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 %.14en" 
        "       Python's value of pi is %.14en"
        "The error in the approximation of pi is %.6en"
        "The number of terms used to calculate the value of pi is %g " %
        (sumPi,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 %.14en" 
        "       Python's value of pi is %.14en"
        "The error in the approximation of pi is %.6en"
        "The number of terms used to calculate the value of pi is %g " %
        (sumPi,count))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读