From: "Jay Blanchard" on
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: Robert Cummings on
On Mon, 2008-07-14 at 12:39 -0500, Jay Blanchard wrote:
> /* 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";
> }

What makes you think the problem is in this code? How about doing the
following right before the block of code you've provided so that we can
actually check the data with what you're using:

<?php

echo '$acct["BALANCE"]: '.$acct["BALANCE"]."\n";
echo '$adjBalance: '.$adjBalance."\n";
echo '$intlLDCharges: '.$intlLDCharges."\n";
echo '$longDistance: '.$longDistance."\n";
echo '$smsCharges: '.$smsCharges."\n";
echo '$roaming: '.$roaming."\n";
echo '$daCharges: '.$daCharges."\n";
echo '$totalData: '.$totalData."\n";
echo '$endingBal: '.$endingBal."\n";

?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

From: "Jay Blanchard" on
[snip]
What makes you think the problem is in this code? How about doing the
following right before the block of code you've provided so that we can
actually check the data with what you're using:

<?php

echo '$acct["BALANCE"]: '.$acct["BALANCE"]."\n";
echo '$adjBalance: '.$adjBalance."\n";
echo '$intlLDCharges: '.$intlLDCharges."\n";
echo '$longDistance: '.$longDistance."\n";
echo '$smsCharges: '.$smsCharges."\n";
echo '$roaming: '.$roaming."\n";
echo '$daCharges: '.$daCharges."\n";
echo '$totalData: '.$totalData."\n";
echo '$endingBal: '.$endingBal."\n";

?>
[/snip]

$adjBalance: 0
$intlLDCharges: 0.2
$longDistance: 0
$smsCharges: 0
$roaming: 0
$daCharges: 0
$totalData: 0
$endingBal: 0.1

I had done this looking for special characters or negative signs.
From: Robert Cummings on
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

From: "Jay Blanchard" on
[snip]
So again... from whence do you conjure $endingBal? :)
[/snip]

$endingBal is conjured from the database tracking the account balance.
For any 24 hour period the beginning and ending balance for subsequent
days is the same unless adjustments or payments have been made to the
account in that period.

2:35 AM balance becomes the ending balance for the previous day and
becomes the beginning balance for the day we are beginning.