In my application I have a procedure which is related with financial balance modification, and, so, it is wrapped in a transaction.
And the end of the operation, just before committing the transaction, I have a small check - to make sure balance was modified correctly.
public function setBalance($amount) {
// ......
// Updating balance code, nothing worst attention
// ......
$balance = $this->getBalance(false);
if ($amount != $balance) {
throw new Exception('Error updating balance! ' . $balance . " instead of " . $amount);
}
This easy code throws the exception. It says 386.57 is not equal 386.57. Really.
I have tried to check what's the difference using my favorite var_dump
and die
.
var_dump($balance - $amount);
die();
Output is even more wonderfull:
float(-2.8421709430404E-13)
Both numbers $balance
and $amount
are float
.
Can someone explain this strange behavior and why is it happening? Is there some mistake?