I wrote a little script that calculates the first digits of pi using polygons. When I print my output like this:
print(pi)
It only prints the first 16 digits. I want to print more digits and tried this:
print("{0:.100f}".format(pi))
Although this does print more digits, these digits aren't correct. They also don't change when I use different sizes of polygons. I think that Python rounds of certain values somewhere, is there a way to prevent this?
This Is my code:
import math
print("--------------------------------")
#ask for the number of sides
n = int(input("Number of sides: "))
#calculate the big, small and approximated value of pi
alfa = math.radians(180)/n
big_pi = n * math.tan(alfa)
small_pi = n * math.sin(alfa)
approx_pi = (big_pi + small_pi)/2
#count how many digits we got right
pi = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
counter = -1
for i in range(0, len(str(approx_pi))):
if pi[i] == str(approx_pi)[i]:
counter += 1
else:
break
#print the results
print("--------------------------------")
print(str(small_pi) + " < pi < " + str(big_pi))
print("--------------------------------")
print("Average of both values: " + str(approx_pi))
print("--------------------------------")
print("first " + str(counter) + " digits correct")
Thank you in advance.