找到你要的答案

Q:Calculate Pi in Python with Polygons

Q:Python中的多边形计算圆周率

I've got a problem in calculating Pi exactly. I'm using a method where I got a circle with radius=1 and inside of it I put polygons with 8, 16, 32, 64, ... corners, 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 it because it's written partially in German :D All it does is taking an Octagon, divide 360° by the number of corners (first 8) and then by 2. An angle of 22.5° is the result which the program now puts into a sine to calculate 1/16 of the Scope of the Octagon, then doubles the result again and multiplies it by the number of corners of the polygon. In the last step (on the top ^^) it doubles the number of corners in the polygon and runs through all these steps again.

Here are some results of 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 a completely equal and wrong. That's the part that is correct: 3.141592653589793 and the rest of it is not. WHY?

Normally it would repeat printing the last line of the result with many many zeros at the end because I set the "getcontext().prec = 500".

What have I done wrong?

I'm not very experienced in Python and programming in general. And please don't show me other possibilities to calculate it because it has to be this one (for school).

UPDATE: Solved the Problem using mpmath module. The error was that the build in math.sin() function isn't precise enough calculating these tiny angles. Just import 'mpmath' instead of 'math' and 'decimal' and write mp.sin().

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

这里是程序:

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 it because it's written partially in German :D All it does is taking an Octagon, divide 360° by the number of corners (first 8) and then by 2. An angle of 22.5° is the result which the program now puts into a sine to calculate 1/16 of the Scope of the Octagon, then doubles the result again and multiplies it by the number of corners of the polygon. In the last step (on the top ^^) it doubles the number of corners in the polygon and runs through all these steps again.

以下是前几个步骤的一些结果:

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

最后2个结果完全相等和错误。这是正确的部分:3.141592653589793,其余的不是。为什么?

通常它会重复打印结果的最后一行结尾处有很多零因为我设置“getcontext()。PREC = 500”。

我做错了什么?

I'm not very experienced in Python and programming in general. And please don't show me other possibilities to calculate it because it has to be this one (for school).

更新:解决了这个问题,使用mpmath模块。这个错误是建立在数学。sin()功能不够精确计算这些微小的角度。只是进口mpmath”而不是“数学”和“十进制”写sin() MP。

answer1: 回答1:

You use getcontext only for Decimal objects, but when you use traditional math functions you lose precision. Just floating points of 64 bits gives 15 - 17 significant decimal digits precision.
Maybe, you can try with sympy module for more precision over symbolic expresions for your approximation.

You use getcontext only for Decimal objects, but when you use traditional math functions you lose precision. Just floating points of 64 bits gives 15 - 17 significant decimal digits precision.
Maybe, you can try with sympy module for more precision over symbolic expresions for your approximation.

python  pi