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';
}
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.