Demo entry 6364933

金王震

   

Submitted by suzui961228 on May 17, 2017 at 12:50
Language: Matlab. Code size: 580 Bytes.

clear;clc;
item=1;sum=1;s=5e-5;a=1;i=1;t=0; %Initialization item作为单项值,sum1作为单精度的值 sum2作为双精度的值
                                         %s作为4位有效数字的误差容限,a作为迭代误差的传递值,t临时存放上一次和的值
                           %获取真值
while(abs(single(item))>eps(sum))                          %
    item=(-1)*item*(2*i-1)/(2*i+1);             %输入公式
    sum=single(sum+item);      %单精度输入                
    er=(sum*4-pi)/pi;                    %单精度与真值的误差
    i=i+1;
end
p=vpa(sum*4,4);                        %四位有效数字的π值


er =

   1.3179745e-06

sum*4 =

   3.1415968

 
p =
 
3.142

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).