Calculate Pi in Python with polygons

I have a problem calculating Pi. I use a method where I have a circle with radius = 1 and inside it I put polygons with corners 8, 16, 32, 64, ..., doubling them after each step. But the problem is that only the first 15 decimal digits of the result are correct.

Here's the program:

import math
import time
from decimal import *

n_Ecken = 8

while n_Ecken >=8:
    time.sleep(0.5)
    n_Ecken = n_Ecken * 2

    def berechneAlpha():
        kreis = 360                                                                 
        alphaInsideFunc = Decimal(kreis) / Decimal(n_Ecken)                         
        return alphaInsideFunc                                                      
    alphaOutsideFunc = berechneAlpha()


    def berechneBeta():                                                             
        betaInsideFunc = Decimal(alphaOutsideFunc) / Decimal(2)                      
        return betaInsideFunc                                                       
    betaOutsideFunc = berechneBeta()                                                


    def berechneKantenlaenge():                                                     
        Gegenkathete = Decimal(math.sin(math.radians(betaOutsideFunc)))             
        KantenlaengeInsideFunc = Gegenkathete * 2                                   
        return KantenlaengeInsideFunc                                               
    KantenlaengeOutsideFunc = berechneKantenlaenge()                                


    def berechneUmfang():                                                           
        UmfangInsideFunc = n_Ecken * KantenlaengeOutsideFunc                        
        return UmfangInsideFunc                                                     
    UmfangOutsideFunc = berechneUmfang()                                            


    def berechnePi():                                                               
        PiInsideFunc = UmfangOutsideFunc / 2                                        
        return PiInsideFunc                                                         

    getcontext().prec = 500
    print Decimal(berechnePi())
    print "Zahl der Ecken:", n_Ecken

      

I hope you can understand this because it is written partially in German: D All it does is take an Octagon, divide 360 ​​° by the number of angles (the first 8) and then by 2. The angle of 22.5 ° is the result. which the program now puts into the sine to calculate 1/16 of the volume of the Octagon, then doubles the result again and multiplies it by the number of corners of the polygon. In the last step (top ^^) it doubles the number of corners in the polygon and goes through all these steps again.

Here are some results from the first few steps:

3.121445152258051969340613141
Zahl der Ecken: 16

3.1365484905459393161208936362527310848236083984375
Zahl der Ecken: 32

3.1403311569547529558121823356486856937408447265625
Zahl der Ecken: 64

3.14127725093277287982118650688789784908294677734375
Zahl der Ecken: 128

3.1415138011443008991818715003319084644317626953125
Zahl der Ecken: 256

3.141572940367091337776628279243595898151397705078125
Zahl der Ecken: 512

3.14158772527715957068039642763324081897735595703125
Zahl der Ecken: 1024

3.141591421511199744287523571983911097049713134765625
Zahl der Ecken: 2048

3.1415923455701175726062501780688762664794921875
Zahl der Ecken: 4096

3.14159257658487245379319574567489326000213623046875
Zahl der Ecken: 8192

3.141592634338562728402166612795554101467132568359375
Zahl der Ecken: 16384

3.141592648776985630121316717122681438922882080078125
Zahl der Ecken: 32768

3.14159265238659113350649931817315518856048583984375
Zahl der Ecken: 65536

3.14159265328899284241970235598273575305938720703125
Zahl der Ecken: 131072

3.1415926535145928255587932653725147247314453125
Zahl der Ecken: 262144

3.14159265357099304338817091775126755237579345703125
Zahl der Ecken: 524288

3.141592653585093319890120255877263844013214111328125
Zahl der Ecken: 1048576

3.14159265358861805594870020286180078983306884765625
Zahl der Ecken: 2097152

3.141592653589499573030252577154897153377532958984375
Zahl der Ecken: 4194304

3.141592653589719841278338208212517201900482177734375
Zahl der Ecken: 8388608

3.141592653589774908340359615976922214031219482421875
Zahl der Ecken: 16777216

3.14159265358978867510586496791802346706390380859375
Zahl der Ecken: 33554432

3.141592653589791783730333918356336653232574462890625
Zahl der Ecken: 67108864

3.141592653589792671908753618481568992137908935546875
Zahl der Ecken: 134217728

3.141592653589793115997963468544185161590576171875
Zahl der Ecken: 268435456

3.141592653589793115997963468544185161590576171875
Zahl der Ecken: 536870912

      

The last 2 results are completely equal and incorrect. This is the correct part: 3.141592653589793 and the rest is not. Why?

It usually repeats printing the last line of the result with many zeros at the end, because I set "getcontext (). Prec = 500".

What I did wrong?

I am not very experienced with Python or programming in general. And please don't show me any other possibilities to calculate this, because it should be one (for the school).

UPDATE: . Fixed issue with using mpmath module. The error was that the build in math.sin () function is not accurate enough in calculating these tiny angles. Just import "mpmath" instead of "math" and "decimal" and write mp.sin ().

+3


source to share


1 answer


You only use getcontext for Decimal objects, but when you use traditional math functions, you lose precision. Just 64 bit floating points give 15 - 17 valid decimal digits. Maybe you can try with the sympy module for more precision over symbolic expressions for your approximation.



0


source







All Articles