php fails at the maths
ugh. I've spent the last few hours fighting a bug that's been driving me up the wall.
So, OK, I get that due to the limitation of floating point accuracy sometimes weird shit happens... like getting 7.9999... when you expected 8.0. But look at the output, they're clearly similar enough that they both render as 1.59... but they're different enough to cause a comparison to fail.
ARGH!
In the end, I used strcmp. :(
marchhair:~ cabbey$ php
<?
$d = 1.59;
$s = (double) "1.59";
if ($d == $s) {
echo "$d and $s are equal\n";
} else {
echo "phpFAILS, $d and $s aren't equal\n";
}
?>
1.59 and 1.59 are equal
marchhair:~ cabbey$ php
<?
$d = 0.99 + 0.60;
$s = (double) "1.59";
if ($d == $s) {
echo "$d and $s are equal\n";
} else {
echo "phpFAILS, $d and $s aren't equal\n";
}
?>
phpFAILS, 1.59 and 1.59 aren't equal
So, OK, I get that due to the limitation of floating point accuracy sometimes weird shit happens... like getting 7.9999... when you expected 8.0. But look at the output, they're clearly similar enough that they both render as 1.59... but they're different enough to cause a comparison to fail.
ARGH!
In the end, I used strcmp. :(
Labels: fail, math, php, software engineering, stupidity, whine
0 Comments:
Post a Comment
<< Home