[英]Can anyone tell me why is my code showing the wrong value of pi?
Here's a picture of my output:
这是我输出的图片:
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))
These are the values it is is showing: 这些是它显示的值:
The approximate value of pi is 3.08770957930231e+00 pi的近似值是3.08770957930231e + 00
Python's value of pi is 3.14159265358979e+00 Python的pi值是3.14159265358979e + 00
I want it to show me this : 我希望它能告诉我这个:
The approximate value of pi is 3.14159265358979 pi的近似值是3.14159265358979
Python's value of pi is 3.14159265358979 Python的pi值是3.14159265358979
I think you're missing signal
. 我想你错过了
signal
。 Apparently you tried to do this but changing the previous term signal and using it on the next term. 显然你试图这样做,但改变前一个术语信号并在下一个学期使用它。 See my code, i tried to do like his.
看到我的代码,我试着像他一样。 What do you think?
你怎么看?
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))
As for me problem is because you change term
value. 至于我的问题是因为你改变了
term
价值。 it has to be 1
or -1
- sign. 它必须是
1
或-1
- 符号。
My version - I use for
loop 我的版本 - 我
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))
Result for 7 terms 结果为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
Result for 15 terms 结果为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
EDIT: version with your while
loop 编辑:带有
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))
Your term
calculation is wrong: 您的
term
计算错误:
term = -term/(divNum * (3**npower))
Say term
is currently -1/(3*3)
. 说
term
目前是-1/(3*3)
。 This line won't set term
to 1/(5 * 3**2)
; 该行不会将
term
设置为1/(5 * 3**2)
; it'll set term
to 1/(3*3) / (5 * 3**2)
. 它将
term
设置为1/(3*3) / (5 * 3**2)
。 You're reducing term
way more than you're supposed to. 你正在减少比你想象的更多的
term
。
It looks like your trying to define term[i+1]
in terms of term[i]
. 看起来你试图用
term[i+1]
定义term[i+1]
term[i]
。 If you change that line to: 如果您将该行更改为:
term = -term*(divNum-2)/(divNum * 3)
then the recursive definition will produce the appropriate values. 那么递归定义将产生适当的值。 This definition will flip the sign, remove the old odd number in the denominator, add the new odd number in the denominator, and add a factor of 3 in the denominator.
该定义将翻转符号,删除分母中的旧奇数,在分母中添加新的奇数,并在分母中添加因子3。
You can use this kind of pattern to generate the denominator for the terms in your approximation. 您可以使用此类模式为近似项生成分母。 I'll let you do the division and summation, and the final multiplication by sqrt(12)
我会让你进行除法和求和,最后乘以sqrt(12)
print [(-1)**(i%2)*(3**(i)*(1+i*2)) for i in range(0,10)]
You are using e
as the number format which means: 您使用
e
作为数字格式,这意味着:
Exponent notation.
指数表示法。 Prints the number in scientific notation using the letter 'e' to indicate the exponent.
使用字母“e”以科学记数法打印数字以表示指数。
If you want fixed point output you can use f
: 如果你想要定点输出你可以使用
f
:
Fixed point.
固定点。 Displays the number as a fixed-point number.
将数字显示为定点数。
Other formats/options can be found in the documentation 其他格式/选项可在文档中找到
Found the bug in your code. 在代码中发现了错误。 Each
term
is calculated using the previous term
as the numerator, when really you just want alternating -1 and 1s. 每个
term
都是使用前一个term
作为分子来计算的,而实际上你只需要交替-1和1。 Changing the formula for calculating term
fixes the issue: 更改计算
term
的公式可以解决问题:
term = ((-1)**npower)/(divNum * (3**npower))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.