Demo entry 6795636

test

   

Submitted by anonymous on May 13, 2019 at 19:13
Language: Matlab. Code size: 462 Bytes.

i = 0;
mypi1 = 0;
e= 1.1921e-07;
while 1
    a = ((-1)^i)/(2*i+1);
    mypi1 = mypi1 + a;
    %%计算误差,满足4位有效数字时结束循环
    if abs(a)<e , break, end 
    i = i+1;
end
mypi1 = 4 * mypi1;
vpa(mypi1,7)
%%计算公式二
k = 1;
mypi2 = 0.5;
b = 0.5;
while 1
    b = ((2*(k-1)+1)^2)/(2*k*(2*k+1)) * b * 0.5^2;
    mypi2 = mypi2 + b;
    %%计算误差,满足4位有效数字时结束循环
    if  abs(b)<e|| abs(6*mypi2 - pi)==e , break, end 
    k = k+1;
end
mypi2 = 6*mypi2;
vpa(mypi2,7)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).