本文介绍了关于浮点精度:为什么迭代次数不相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有两个类似的matlab程序,一个迭代10次,而另一个迭代11次。 i = 0; x = 0.0; h = 0.1; ,而x i = i + 1; x = i * h; disp([i,x]); end 另外: i = 0; x = 0.0; h = 0.1; ,而x 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 >> 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有关): a> 浮点数精度 b $ b 如何确定中的错误是否是我的答案的错误或错误? COLON运算符如何工作? 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]);endAnother:i = 0;x = 0.0;h = 0.1;while x < 1.0 i = i + 1; x = x + h; disp([i,x]);endI 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.100000000000000010000.200000000000000010000.300000000000000040000.400000000000000020000.500000000000000000000.600000000000000090000.700000000000000070000.800000000000000040000.900000000000000020001.00000000000000000000>> fprintf('%0.20f\n', cumsum(repmat(0.1,1,10)))0.100000000000000010000.200000000000000010000.300000000000000040000.400000000000000020000.500000000000000000000.599999999999999980000.699999999999999960000.799999999999999930000.899999999999999910000.99999999999999989000Also compare against using MATLAB's COLON operator:>> fprintf('%0.20f\n', 0.1:0.1:1)0.100000000000000010000.200000000000000010000.300000000000000040000.400000000000000020000.500000000000000000000.599999999999999980000.699999999999999960000.800000000000000040000.900000000000000020001.00000000000000000000If 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 3fefffffffffffffSome suggested readings (MATLAB related):Cleve's Corner 1996 articleA Glimpse into Floating-PointAccuracyHow do I determine if the error inmy answer is the result of round-offerror or a bug?How does the COLON operatorwork? 这篇关于关于浮点精度:为什么迭代次数不相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-15 04:46