<?php
echo 53.96-(44.98+8.98);
?>这段代码的输出是 7.105427357601E-15
在php5.25里的结果。请高手解释一下为什么?

解决方案 »

  1.   

    要正确结果,请试试:
    echo (53.96-(44.98+8.98)); 
      

  2.   

    请有兴趣的兄弟在不同环境下验证一下,有是等于0的吗? 难道是个bug?这都被我碰上了, 汗。
      

  3.   

    用round(53.96-(44.98+8.98),0)来输出就可以了。这样精度就不会有问题
      

  4.   

    关于浮点型运算,手册里有这么一段加黄标柱的:Floating point precision: 
    It is quite usual that simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a little loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8 as the result of the internal representation really being something like 7.9999999999.... This is related to the fact that it is impossible to exactly express some fractions in decimal notation with a finite number of digits. For instance, 1/3 in decimal form becomes 0.3333333. . .. So never trust floating number results to the last digit and never compare floating point numbers for equality. If you really need higher precision, you should use the arbitrary precision math functions or gmp functions instead.
    不要信任浮点型数字的计算结果
      

  5.   

    不光是PHP,很多程式都有这种问题