且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于浮点精度:为什么迭代次数不相等?

更新时间:2023-11-12 14:11:34

比较以下输出:

 >> 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




 >> 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):