jon roig wrote:
Ok... It's Friday and maybe my brain is dead, but I'm having a weird
problem with some basic math.

Here's a little snippet of the code I'm working with:

---------------------------
echo "<p>Current:$currentAmount:".gettype($currentAmount)." -
Paid:$paidAmount:".gettype($paidAmount)."</p>";

$currentAmount = $currentAmount-$paidAmount;
echo "<p>Final Current: $currentAmount";
-----------------

Straightforward... Yeah?

Here's a sample of the output:
Current:97.6:double - Paid:97.6:double
Final Current: 1.42108547152E-014

Now if both $currentAmount and $paidAmount are doubles, subtracting them
should yield a zero, shouldn't it?


No, because they are doubles. If they were integers it would yield a zero. Try to compare the doubles and you find out they are not equal:


if($currentAmount == $paidAmount) echo "equal<br>";
else echo "not equal<br>";

You need to know how are floats and doubles represented. They are in fact something like 97.5999989 or 97.6000001, so you should never compare doubles and floats if they are equal, or you should round them before.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to