For my school project I was trying to compute the value of using different methods. One of the formula I found was the Machin Formula that can be calculated using the Taylor expansion of arctan(x).
I wrote the following code in python:
import decimal count = pi = a = b = c = d = val1 = val2 = decimal.Decimal(0) #Initializing the variables decimal.getcontext().prec = 25 #Setting percision while (decimal.Decimal(count) <= decimal.Decimal(100)): a = pow(decimal.Decimal(-1), decimal.Decimal(count)) b = ((decimal.Decimal(2) * decimal.Decimal(count)) + decimal.Decimal(1)) c = pow(decimal.Decimal(1/5), decimal.Decimal(b)) d = (decimal.Decimal(a) / decimal.Decimal(b)) * decimal.Decimal(c) val1 = decimal.Decimal(val1) + decimal.Decimal(d) count = decimal.Decimal(count) + decimal.Decimal(1) #The series has been divided into multiple small parts to reduce confusion count = a = b = c = d = decimal.Decimal(0) #Resetting the variables while (decimal.Decimal(count) <= decimal.Decimal(10)): a = pow(decimal.Decimal(-1), decimal.Decimal(count)) b = ((decimal.Decimal(2) * decimal.Decimal(count)) + decimal.Decimal(1)) c = pow(decimal.Decimal(1/239), decimal.Decimal(b)) d = (decimal.Decimal(a) / decimal.Decimal(b)) * decimal.Decimal(c) val2 = decimal.Decimal(val2) + decimal.Decimal(d) count = decimal.Decimal(count) + decimal.Decimal(1) #The series has been divided into multiple small parts to reduce confusion pi = (decimal.Decimal(16) * decimal.Decimal(val1)) - (decimal.Decimal(4) * decimal.Decimal(val2)) print(pi) The problem is that I am getting the right value of pi only till 15 decimal places, no matter the number of times the loop repeats itself.
For example:
at 11 repetitions of the first loop
pi = 3.141592653589793408632493
at 100 repetitions of the first loop
pi = 3.141592653589793410703296
I am not increasing the repetitions of the second loop as arctan(1/239) is very small and reaches an extremely small value with a few repetitions and therefore should not affect the value of pi at only 15 decimal places.
EXTRA INFORMATION:
The Machin Formula states that:
π = (16 * Summation of (((-1)^n) / 2n+1) * ((1/5)^(2n+1))) - (4 * Summation of (((-1)^n) / 2n+1) * ((1/239)^(2n+1)))