This question already has answers here:
Why can't decimal numbers be represented exactly in binary?
                                
                                    (20个答案)
                                
                        
                        
                            Floating point arithmetic not producing exact results [duplicate]
                                
                                    (7个答案)
                                
                        
                                6年前关闭。
            
                    
我只是试图计算percent_imp,但不是0.22(精确为0.22,没有舍入错误),而是得到0.22000000000000003!
我曾经得到类似的奇数结果,并且被告知要从float变为double,但是这个仍然很奇怪!
以下所有变量都是双精度!

double percentage_imp= budget -  (sum_minlessi)/ (sum_i + sum_lessi);

最佳答案

那是因为浮点精度值。

您必须阅读:-What Every Computer Scientist Should Know About Floating-Point Arithmetic

您还必须阅读how floating point arithmetic and its internal representation works

09-25 20:53