简体   繁体   English

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

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

这是我试图在while循环中使用的等式 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))

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能告诉我这里的代码有什么问题? - Can anyone tell me what's wrong with my code here? 谁能告诉我为什么我的管道错了? - Can anyone tell me why is my pipeline wrong? 谁能告诉我我的代码有什么问题? 一个测试用例失败 - Can anyone tell me what is wrong with my code? One test case is failing 谁能告诉我我的功能出了什么问题? - Can anyone tell me what's wrong with my function? 谁能告诉我我的 if 条件有什么问题? - can anyone tell me what is wrong with my if condition? 谁能告诉我如何在我的代码中增加蛇的大小? - Can anyone tell me how to increase the snake size in my code? 代码跳过 Python 中的命令。 谁能告诉我为什么? - The code skips a command in Python. Can anyone tell me why? 我的 function 没有正确定义,谁能告诉我为什么? - My function is not defined properly, can anyone tell me why? PYTHON 问题:为什么我的代码在行号时显示“NONE”。 9被处决? 谁能帮助我? - PYTHON Problem: Why is my code showing “NONE” when line no. 9 is executed? can anyone assist me? 在 GOOGLE COLAB 中运行此 python 代码时,它向我显示错误。 谁能告诉我我做错了什么并分享更正的代码? - On running this python code in GOOGLE COLAB, it showing me error. Can anyone please tell what am i doing wrong and share a corrected code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM