0

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.

M.DeBaecke
  • 83
  • 2
  • 10
  • Doesn’t radians(…) already involve a computation with pi which basically renders the computation a bit mood / self-fulfilling? – luk2302 Aug 26 '21 at 09:25
  • @luk2302, yes, the calculation itself is quite useless, tan and sin also use pi. I was just playing around with it. Thank you for the link, that does seem to solve the rounding problem. – M.DeBaecke Aug 26 '21 at 09:32

0 Answers0