Demo entry 6361874

数值计算方法

   

Submitted by suzui961228 on May 05, 2017 at 15:51
Language: Matlab. Code size: 610 Bytes.

sum=0;    %单精度从上到下进行计算
sum1=single(sum);
for i=1:10000     
    item=single(1/i^4);
    sum1=sum1+item;       %sum1作为输出
end

sum=0;   %单精度从下到上进行计算
sum2=single(sum);
for i=10000:-1:1
    item=single(1/i^4);
    sum2=sum2+item;       %sum2作为输出
end

sum=0;   %双精度从上到下进行计算
sum3=double(sum);
for i=1:10000
    item=double(1/i^4);
    sum3=sum3+item;       %sum3作为输出
end

sum=0;  %双精度从下到上进行计算
sum4=double(sum);
for i=10000:-1:1
    item=double(1/i^4);
    sum4=sum4+item;       %sum4作为输出
end

%输出结果
sum1 = 1.0823221
sum2 = 1.0823232
sum3 = 1.082323233710861
sum4 = 1.082323233710805

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).