On Mon, 2008-07-14 at 12:39 -0500, Jay Blanchard wrote:
> 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?
>From whence do you conjure $endingBal?
Most likely this is just a floating point imprecision problem. In
otherwords, you need a small delta of error when doing the following
comparison:
if( $totBalDiff === $endingBal )
Remember, floating point numbers do not always store perfectly. So you
want something like:
if( abs( $totalBalDiff - $endingBal ) > .000001 )
Also that last echo:
echo number_format($totChargeDiff, 2, '.', '')
Was calculated with:
$totChargeDiff = $endingBal - $totalCharges;
Whereas the initial conditional checks:
$totBalDiff === $endingBal
And $totBalDiff was calculated as:
$totBalDiff = $totalBalance - $totalCharges;
So again... from whence do you conjure $endingBal? :)
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php