Math Weirdness

From: Date: Mon, 14 Jul 2008 17:39:25 +0000
Subject: Math Weirdness
Groups: php.general 
Request: Send a blank email to php-general+get-276754@lists.php.net to get a copy of this message
I am totally buffaloed by a set of very simple calculations that I am doing; /* calculate total balance with payments and adjustments */ $totalBalance = $acct['BALANCE'] + $adjBalance; echo number_format($totalBalance, 2, '.', '')."\t"; /* calculate total charges */ $totalCharges = $intlLDCharges + $longDistance + $smsCharges + $daCharges + $totalData + $roaming; echo number_format($totalCharges, 2, '.', '')."\t"; /* * calculate difference between total balance and total charges * if the amount matches the ending balance then all is OK * if not calculate the difference */ $totBalDiff = $totalBalance - $totalCharges; if($totBalDiff === $endingBal){ echo "OK\t"; } else { /* what is the difference between the ending balance and the charges? */ $totChargeDiff = $endingBal - $totalCharges; echo number_format($totChargeDiff, 2, '.', '')."\t"; } Each number represented by a variable in all of these calculations has been rounded to 2 decimal points at the point they are generated. For the most part this works just hunky-dory but I have a handful of calcs (out of 300k plus records) that look like this.... $endingBal 0.10 $totalBalance 0.30 $totalCharges 0.20 $totalChargeDiff -0.10 The balance minus the charges does equal the ending balance as it should but it is saying that it doesn't and that there a 20 cent swing (-0.10 is 20 cents different than 0.10). I must be missing something. When I echo out raw data I do not see negative signs. Does anyone have any insight as to what might be happening here?

« previous php.general (#276754) next »