Can someone tell me why my code is showing the wrong pi value?

This is the equation I'm trying to use in a while loop Here is a picture of my output:

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 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 ​​that it shows:

The approximate value of pi is 3.08770957930231e + 00

The python value for pi is 3.14159265358979e + 00

I want it to seem to me:

The approximate value of pi is 3.14159265358979

The python value for pi is 3.14159265358979

+4
source share
6 answers

For me, the problem is that you are changing the meaning term. it must be 1or -1- sign.

My version - I use a 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 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

The approximate value of pi is 3.14167431269884e+00
       Python 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

The approximate value of pi is 3.14159265952171e+00
       Python 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 loopwhile

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

, 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 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))
+1

term :

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

term -1/(3*3). term 1/(5 * 3**2); term 1/(3*3) / (5 * 3**2). term , .

0

, term[i+1] term[i]. :

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

. , , .

0

. , sqrt (12)

print [(-1)**(i%2)*(3**(i)*(1+i*2)) for i in range(0,10)]
0

e , :

. , "e", .

, f:

. .

/


Found an error in your code. Each is termcalculated using the previous one termas a numerator, when really you just want to alternate -1 and 1s. Changing the formula for the calculation termfixes the problem:

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

Demo

-1
source

Source: https://habr.com/ru/post/1626264/


All Articles