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

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

概述这是我输出的图片: inptTol = float(input("Enter the tolerance: "))print()term = 1divNum = 3npower = 1sumPi = 0.0count = 0while abs(term) > inptTol: sumPi += term term = -term/(divNum * (3**npo

这是我输出的图片:

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

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

我的版本 – 我用于循环

import mathterms_number = float(input("Enter terms number: "))sign = 1divNum = 1npower = 0sumPi = 0.0count = 0for x in range(terms_number):    sumPi += sign/(divNum * (3**npower))    # values for next term    sign = -sign    divNum += 2    npower += 1    count += 1sumPi = 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,count))

结果为7个学期

The approximate value of pi is 3.14167431269884e+00       Python's value of pi is 3.14159265358979e+00The error in the approximation of pi is 8.165911e-05The 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+00The error in the approximation of pi is 5.931921e-09The number of terms used to calculate the value of pi is 15

编辑:带有while循环的版本

import mathinptTol = float(input("Enter the tolerance: "))term = 1sign = 1divNum = 1npower = 0sumPi = 0.0count = 0while abs(term) > inptTol:    term = sign/(divNum * (3**npower))    sumPi += term    # values for next term    sign = -sign    divNum += 2    npower += 1    count += 1sumPi = 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,count))
总结

以上是内存溢出为你收集整理的python – 任何人都可以告诉我为什么我的代码显示错误的pi值?全部内容,希望文章能够帮你解决python – 任何人都可以告诉我为什么我的代码显示错误的pi值?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1196053.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存