关于浮点精度:为什么迭代次数不相等? [英] About floating point precision: why the iteration numbers are not equal?

查看:154
本文介绍了关于浮点精度:为什么迭代次数不相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个类似的matlab程序,一个迭代10次,而另一个迭代11次。

  i = 0; 
x = 0.0;
h = 0.1;
,而x < 1.0
i = i + 1;
x = i * h;
disp([i,x]);
end

另外:

  i = 0; 
x = 0.0;
h = 0.1;
,而x < 1.0
i = i + 1;
x = x + h;
disp([i,x]);
end

我不明白为什么浮点加操作和

解决方案

比较以下输出:

 >> fprintf('%0.20f \ n',0.1。*(1:10))
0.10000000000000001000
0.20000000000000001000
0.30000000000000004000
0.40000000000000002000
0.50000000000000000
0.60000000000000009000
0.70000000000000007000
0.80000000000000004000
0.90000000000000002000
1.00000000000000000000

>> fprintf('%0.20f\\\
',cumsum(repmat(0.1,1,10)))
0.10000000000000001000
0.20000000000000001000
0.30000000000000004000
0.40000000000000002000
0.50000000000000000 $
0.59999999999999998000
0.69999999999999996000
0.79999999999999993000
0.89999999999999991000
0.99999999999999989000



<比较使用MATLAB的COLON算子:

 >> fprintf('%0.20f \ n',0.1:0.1:1)
0.10000000000000001000
0.20000000000000001000
0.30000000000000004000
0.40000000000000002000
0.50000000000000000000
0.59999999999999998000
0.69999999999999996000
0.80000000000000004000
0.90000000000000002000
1.00000000000000000000

如果您想要请参阅64位二进制表示法,使用:

 >> format hex 
>> [(0.1:0.1:1)'(0.1。*(1:10))'cumsum(repmat(0.1,10,1))]
3fb999999999999a 3fb999999999999a
3fc999999999999a 3fc999999999999a 3fc999999999999a
3fd3333333333334 3fd3333333333334 3fd3333333333334
3fd999999999999a 3fd999999999999a 3fd999999999999a
3fe0000000000000 3fe0000000000000 3fe0000000000000
3fe3333333333333 3fe3333333333334 3fe3333333333333
3fe6666666666666 3fe6666666666667 3fe6666666666666
3fe999999999999a 3fe999999999999a 3fe9999999999999
3feccccccccccccd 3feccccccccccccd 3feccccccccccccc
3ff0000000000000 3ff0000000000000 3fefffffffffffff

一些建议阅读(与MATLAB有关):


There are two similar matlab programs, one iterates 10 times while another iterates 11 times.

One:

i = 0;
x = 0.0;
h = 0.1;
while x < 1.0
    i = i + 1;
    x = i * h;
    disp([i,x]);
end

Another:

i = 0;
x = 0.0;
h = 0.1;
while x < 1.0
    i = i + 1;
    x = x + h;
    disp([i,x]);
end

I don't understand why there is difference between the floating point add operation and the multiple.

解决方案

Compare the output of the following:

>> fprintf('%0.20f\n', 0.1.*(1:10))
0.10000000000000001000
0.20000000000000001000
0.30000000000000004000
0.40000000000000002000
0.50000000000000000000
0.60000000000000009000
0.70000000000000007000
0.80000000000000004000
0.90000000000000002000
1.00000000000000000000

>> fprintf('%0.20f\n', cumsum(repmat(0.1,1,10)))
0.10000000000000001000
0.20000000000000001000
0.30000000000000004000
0.40000000000000002000
0.50000000000000000000
0.59999999999999998000
0.69999999999999996000
0.79999999999999993000
0.89999999999999991000
0.99999999999999989000

Also compare against using MATLAB's COLON operator:

>> fprintf('%0.20f\n', 0.1:0.1:1)
0.10000000000000001000
0.20000000000000001000
0.30000000000000004000
0.40000000000000002000
0.50000000000000000000
0.59999999999999998000
0.69999999999999996000
0.80000000000000004000
0.90000000000000002000
1.00000000000000000000

If you want to see the 64-bit binary representation, use:

>> format hex
>> [(0.1:0.1:1)' (0.1.*(1:10))' cumsum(repmat(0.1,10,1))]
   3fb999999999999a   3fb999999999999a   3fb999999999999a
   3fc999999999999a   3fc999999999999a   3fc999999999999a
   3fd3333333333334   3fd3333333333334   3fd3333333333334
   3fd999999999999a   3fd999999999999a   3fd999999999999a
   3fe0000000000000   3fe0000000000000   3fe0000000000000
   3fe3333333333333   3fe3333333333334   3fe3333333333333
   3fe6666666666666   3fe6666666666667   3fe6666666666666
   3fe999999999999a   3fe999999999999a   3fe9999999999999
   3feccccccccccccd   3feccccccccccccd   3feccccccccccccc
   3ff0000000000000   3ff0000000000000   3fefffffffffffff

Some suggested readings (MATLAB related):

这篇关于关于浮点精度:为什么迭代次数不相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆