Abs() - issue with absolute value function in PHP

為{幸葍}努か 提交于 2019-12-11 03:49:21

问题


Can anyone explain why this code does this in regards to abs() (absolute value) - In my code it will display 'GREATER' - although 0.50 is never GREATER than 0.5, am I missing out something here with the abs function?

$logic = abs(1.83333333333 - 2.33333333333);  // 0.50
$limit = 0.50;

if ($logic > $limit) {
    echo 'IS GREATER';
} else {
    echo 'IS NOT GREATER';
}

回答1:


Passing floating point numbers to abs you will get a floating point number as result. In that case you can experience problems with the floating point representation: a floating point is never absolutely precise, thus you are most likely getting a number that is not exactly 0.50 but something like 0.500000...01. You could try to round the result to the desired precision (in your case I guess it is two) with the php round function.




回答2:


If you don't want to round as suggested by @Aldo's answer and your server supports the GMP math functions, you could use gmp_abs() instead. This way you don't run into PHP's inherent floating point problems.




回答3:


Due to the way floating point math works, your absolute value $logic results in this value:

0.50000000000000022204

which is greater than 0.5

NB: above evaluated using Javascript which uses double precision math for all numbers:

Math.abs(1.83333333333 - 2.33333333333).toFixed(20)



回答4:


Never compare floats by equality - user the epsilon technique instead PHP: Floating Point Numbers

define('EPSILON', 1.0e-8);
$logic = abs(1.83333333333 - 2.33333333333);  // 0.50
$limit = 0.50;
$diff = $logic - $limit;
if (abs($diff) < EPSILON)
   echo 'IS EQUAL';
else
   echo 'IS NOT EQUAL';


来源:https://stackoverflow.com/questions/8804842/abs-issue-with-absolute-value-function-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!