当前位置: > Coding > Python >

python 制作CPU压力测试——圆周率计算

时间:2020-10-30 13:28来源:linux.it.net.cn 作者:IT

众所周知,反正弦函数asin有以下公式(如在计算器中是弧度制下):

\frac{\pi}{2}=arcsin\left ( 1 \right )

百度百科搜索三角函数公式,在泰勒展开式一节中有以下公式:

(doubleFact(2*k-1)/doubleFact(2*k))*(pow(x,2*k+1)/(2*k+1))
其中arcsin写错了,(2k+1)!!应该是(2k-1)!!
另外x!!指双阶乘,不同于阶乘
 

双阶乘实现:


  1.  
    def doubleFact(x):
  2.  
    ans=1
  3.  
    for i in range(1,x+1):
  4.  
    if i%2 == x%2:
  5.  
    ans*=i
  6.  
    return ans

asin函数实现


  1.  
    def asin(x,t):
  2.  
    answer=0
  3.  
    for k in range(0,t+1):
  4.  
    a=(doubleFact(2*k-1)/doubleFact(2*k))*(pow(x,2*k+1)/(2*k+1))
  5.  
    print("k=%d,a=%s"%(k,a))
  6.  
    answer+=a

其中x为角度(弧度),t为计算次数。

注:代码奇慢无比,不要真正用来计算pi,又慢精度又不高,建议只用来编写CPU压力测试程序!

总代吗贴出来:


  1.  
    # import math
  2.  
    def doubleFact(x):
  3.  
    ans=1
  4.  
    for i in range(1,x+1):
  5.  
    if i%2 == x%2:
  6.  
    ans*=i
  7.  
    return ans
  8.  
    def asin(x,t):
  9.  
    answer=0
  10.  
    for k in range(0,t+1):
  11.  
    a=(doubleFact(2*k-1)/doubleFact(2*k))*(pow(x,2*k+1)/(2*k+1))
  12.  
    print("k=%d,a=%s"%(k,a))
  13.  
    answer+=a
  14.  
     
  15.  
    return answer
  16.  
    print(asin(1,100)*2)

计算结果:


  1.  
    k=0,a=1.0
  2.  
    k=1,a=0.16666666666666666
  3.  
    k=2,a=0.07500000000000001
  4.  
    k=3,a=0.04464285714285714
  5.  
    k=4,a=0.030381944444444444
  6.  
    k=5,a=0.022372159090909092
  7.  
    k=6,a=0.017352764423076924
  8.  
    k=7,a=0.013964843749999999
  9.  
    k=8,a=0.011551800896139705
  10.  
    k=9,a=0.009761609529194078
  11.  
    k=10,a=0.008390335809616815
  12.  
    k=11,a=0.007312525873598845
  13.  
    k=12,a=0.006447210311889649
  14.  
    k=13,a=0.005740037670841923
  15.  
    k=14,a=0.005153309682319904
  16.  
    k=15,a=0.004660143486915096
  17.  
    k=16,a=0.004240907093679363
  18.  
    k=17,a=0.003880964558837669
  19.  
    k=18,a=0.0035692053938259347
  20.  
    k=19,a=0.0032970595034734844
  21.  
    k=20,a=0.0030578216492580306
  22.  
    k=21,a=0.002846178401108942
  23.  
    k=22,a=0.00265787063820729
  24.  
    k=23,a=0.0024894486782468836
  25.  
    k=24,a=0.002338091892111975
  26.  
    k=25,a=0.0022014739737101384
  27.  
    k=26,a=0.002077661032518167
  28.  
    k=27,a=0.0019650336162772837
  29.  
    k=28,a=0.0018622264064031273
  30.  
    k=29,a=0.0017680811205154183
  31.  
    k=30,a=0.0016816093935831068
  32.  
    k=31,a=0.0016019632753514438
  33.  
    k=32,a=0.0015284115961225677
  34.  
    k=33,a=0.0014603208940791154
  35.  
    k=34,a=0.0013971399176302534
  36.  
    k=35,a=0.0013383869512751784
  37.  
    k=36,a=0.0012836393876290285
  38.  
    k=37,a=0.001232525098500017
  39.  
    k=38,a=0.0011847152561624392
  40.  
    k=39,a=0.0011399183307022236
  41.  
    k=40,a=0.0010978750465914472
  42.  
    k=41,a=0.001058354125872243
  43.  
    k=42,a=0.0010211486797106276
  44.  
    k=43,a=0.0009860731369833312
  45.  
    k=44,a=0.0009529606197429564
  46.  
    k=45,a=0.0009216606921836336
  47.  
    k=46,a=0.0008920374230917098
  48.  
    k=47,a=0.0008639677124658675
  49.  
    k=48,a=0.000837339841602712
  50.  
    k=49,a=0.0008120522129086703
  51.  
    k=50,a=0.0007880122513582057
  52.  
    k=51,a=0.0007651354441371649
  53.  
    k=52,a=0.0007433444987958959
  54.  
    k=53,a=0.0007225686033525614
  55.  
    k=54,a=0.0007027427743614913
  56.  
    k=55,a=0.0006838072810965503
  57.  
    k=56,a=0.0006657071357767537
  58.  
    k=57,a=0.000648391641245871
  59.  
    k=58,a=0.0006318139887619102
  60.  
    k=59,a=0.0006159308995984751
  61.  
    k=60,a=0.0006007023050422869
  62.  
    k=61,a=0.0005860910601175613
  63.  
    k=62,a=0.0005720626870011989
  64.  
    k=63,a=0.0005585851446315293
  65.  
    k=64,a=0.0005456286214729855
  66.  
    k=65,a=0.0005331653487922461
  67.  
    k=66,a=0.0005211694321385132
  68.  
    k=67,a=0.0005096166990104013
  69.  
    k=68,a=0.000498484560941636
  70.  
    k=69,a=0.0004877518884534234
  71.  
    k=70,a=0.000477398897508034
  72.  
    k=71,a=0.000467407046260082
  73.  
    k=72,a=0.00045775894104274026
  74.  
    k=73,a=0.00044843825064875664
  75.  
    k=74,a=0.0004394296280731444
  76.  
    k=75,a=0.00043071863897800795
  77.  
    k=76,a=0.0004222916962219453
  78.  
    k=77,a=0.0004141359998684339
  79.  
    k=78,a=0.0004062394821508707
  80.  
    k=79,a=0.00039859075692766545
  81.  
    k=80,a=0.00039117907320994984
  82.  
    k=81,a=0.0003839942723879085
  83.  
    k=82,a=0.00037702674882018997
  84.  
    k=83,a=0.000370267413484946
  85.  
    k=84,a=0.0003637076604213038
  86.  
    k=85,a=0.0003573393357169886
  87.  
    k=86,a=0.0003511547088217658
  88.  
    k=87,a=0.00034514644598773823
  89.  
    k=88,a=0.00033930758565660254
  90.  
    k=89,a=0.00033363151563102445
  91.  
    k=90,a=0.0003281119518825554
  92.  
    k=91,a=0.00032274291886219894
  93.  
    k=92,a=0.00031751873119201474
  94.  
    k=93,a=0.0003124339766271837
  95.  
    k=94,a=0.00030748350018788655
  96.  
    k=95,a=0.00030266238936928896
  97.  
    k=96,a=0.0002979659603459907
  98.  
    k=97,a=0.0002933897450945759
  99.  
    k=98,a=0.0002889294793644786
  100.  
    k=99,a=0.00028458109143332955
  101.  
    k=100,a=0.0002803406915883404
  102.  
    3.0292687382573695

(责任编辑:IT)
------分隔线----------------------------