1

What would be the most "good practice" way of dealing with php's wrong calculations of decimal numbers ? Please look at example below:

$avg = 311.345;
$delta = 88.645;

$value = 222.7;

if ($value == $avg-$delta) { // $avg - $delta is EXACTLY 222.7
    echo 'TRUE';
} else {
    echo 'FALSE';
}

This is supposed to echo TRUE while it echoes out FALSE.

Now .. i do understand if i use a workaround such as

if ($value == round($avg-$delta, 2))

the problem will go away

However, using workarounds is one thing i hate, so my question is .. is there a good and absolutely correct way to handle this ? something like adjusting some values in php.ini or adding installing some extension(s) for PHP , or something amongst those lines ?

  • There's nothing "wrong" with it, because those are *floats*, not "decimals". http://floating-point-gui.de – deceze Sep 09 '14 at 05:27
  • Dealing with the problems of floating point is something every computer programmer should learn in the first year of classes. – Barmar Sep 09 '14 at 05:28

0 Answers0