Search code examples
pythonpi

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


This is the equation i'm trying to use in the while loopHere's 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'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

Python's value of pi is 3.14159265358979e+00

I want it to show me this :

The approximate value of pi is 3.14159265358979

Python's value of pi is 3.14159265358979


Solution

  • As for me problem is because you change term value. it has to be 1 or -1 - sign.

    My version - I use for loop

    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

    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

    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

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