php中浮点数计算所得的值与直接赋予的值比较
2014-07-05 11:16
786 查看
form:http://www.php.net/manual/zh/language.types.float.php
$x = 8 - 6.4; // which is equal to 1.6
$y = 1.6;
var_dump($x == $y); // is not true
PHP thinks that 1.6 (coming from a difference) is not equal to 1.6. To make it work, use round()
var_dump(round($x, 2) == round($y, 2)); // this is true
This happens probably because $x is not really 1.6, but 1.599999.. and var_dump shows it to you as being 1.6.
$x = 8 - 6.4; // which is equal to 1.6
$y = 1.6;
var_dump($x == $y); // is not true
PHP thinks that 1.6 (coming from a difference) is not equal to 1.6. To make it work, use round()
var_dump(round($x, 2) == round($y, 2)); // this is true
This happens probably because $x is not really 1.6, but 1.599999.. and var_dump shows it to you as being 1.6.
相关文章推荐
- PHP之浮点数计算比较以及取整数不准确的解决办法
- PHP 浮点数计算比较及取整不准确的解决方法
- PHP中浮点数计算比较及取整不准确的解决方法
- PHP中浮点数计算比较及取整不准确的解决方法
- PHP之浮点数计算比较以及取整数不准确的解决办法
- PHP 浮点数计算比较及取整不准确的解决方法
- PHP浮点数计算
- php浮点数计算比较及取整不准确解决方法
- 浮点数不可以直接用“==”和“!=”进行比较
- PHP浮点数比较大小
- PHP时间比较和时间差如何计算
- php浮点数计算问题
- PHP 浮点数比较
- php-Arrays 函数-array_intersect_key-使用键名比较计算数组的交集
- php浮点数计算比较及取整不准确解决方法
- PHP时间比较和时间差如何计算
- linux bash bc awk 浮点 计算 比较
- php-Arrays 函数-array_diff_ukey-用回调函数对键名比较计算数组的差集
- php-Arrays 函数-array_diff_key-使用键名比较计算数组的差值
- OpenJudge计算概论-求一元二次方程的根【含复数根的计算、浮点数与0的大小比较】