I know many of you all know pretty well that floating point precision and computers don't play nicely with each other. I think I learned about it in the first book on 'C' I ever read, but I had never really been bitten by it until today. I was working with a piece of code today at the office that was throwing an error saying two values weren't zeroing out when they clearly should have been. I narrowed down the problem and after a bit of time decided that 81.4 is a terrible, terrible number. For example:
<?php$total =
100 -
81.
4;
//$total should be 18.6echo "18.6 == {$total}? " .
($total ==
18.
6 ?
'yes' :
'no') .
"\n";
var_dump(18.
6 -
$total);
?>
So, let that be a lesson to us all. No matter how simple the calculation, don't blindly trust floats.