I ran into an interesting math error
The actual code producing the error:
[code=php]<?php
// Calculate Balance
$q = 0;
while ($q < $i){
$subtotal = $subtotal + $payment_deposit_[$q];
$q++;
}
settype($thousand, ‘double’); $thousand = 1000.00;
settype($pmt, ‘double’);
$pmt = $thousand – $subtotal;
?>
when … $payment_deposit_[$q] … is 1000
$pmt produces … (-4.54747350886E-13) …
and is supposed to produce … 0 …
I had to add: if($pmt < 0){$pmt = 0;} just to get it to work properly.
I was wondering why it was doing this. I tried using gettype and settype to
troubleshoot the problem without success.
[code=php]<?php
// Begin main display
// retrieve all records from $table_name sort asc item
include("config_credit_card.php");
// create the SQL statement
$sql = "SELECT *FROM $table_name
ORDER BY item
DESC";
$result = mysql_query($sql, $conn) or die(mysql_error());
// define temporary variables as arrays
$item_ = array();
$date_ = array();
// $check_number_ = array();
$description_ = array();
$account_ = array();
$memo_ = array();
$payment_deposit_ = array();
$balance_ = array();
$user_ = array();
$confirm_ = array();
$i = 0;
while ($newArray = mysql_fetch_array($result)) {
$item_[$i] = $newArray['item'];
$date_[$i] = $newArray['date'];
// $check_number_[$i] = $newArray['check_number'];
$description_[$i] = $newArray['description'];
$account_[$i] = $newArray['account'];
$memo_[$i] = $newArray['memo'];
$payment_deposit_[$i] = $newArray['payment_deposit'];
$balance_[$i] = $newArray['balance'];
$user_[$i] = $newArray['user'];
$confirm_[$i] = $newArray['confirm'];
$i++;
}
?>
<?php
// Calculate Balance
$q = 0;
while ($q < $i){
$subtotal = $subtotal + $payment_deposit_[$q];
settype($payment_deposit_[$q], 'float');
// /////////////////////// temporary troubleshooting ///////////////////
echo $payment_deposit_[$q]; echo " : ";
// ////////////////////////////////////////////////////////////////////////
$q++;
}
settype($thousand, 'float'); $thousand = 1000.00;
settype($pmt, 'float');
$pmt = $thousand - $subtotal;
// if($pmt < 0){$pmt = 0;}
?>
<table border="1" width="100%">
<tr><td colspan="5" align="right">Balance</td>
<td align="right"><?php echo $subtotal; ?></td>
<td align="right">(<?php echo $pmt; ?>)</td></tr>
<tr>
<th width="10">Item</th>
<th width="60">Date</th>
<th width="200">Description</th>
<th width="75">Account</th>
<th width="75">Memo</th>
<th width="125">Payment/Deposit Amount</th>
<th width="10">*</th>
</tr>
<form method="post" action="<?php $_SERVER[PHP_SELF]; ?>">
<tr>
<td>
<td><input type="text" size="10" name="date" value="<?php echo $date_formatted; ?>">
<td><input type="text" size="30" maxlength="100" name="description">
<td>
<select name="account">
<option selected value="1"> <?php echo $account_name[1]; ?> </option>
<option value="101"> <?php echo $account_name[2]; ?> </option>
<option value="102"> <?php echo $account_name[3]; ?> </option>
<option value="103"> <?php echo $account_name[4]; ?> </option>
</select>
<?php
// Set up accounts
//
?>
<td><input type="text" size="10" maxlength="50" name="memo">
<td align="right"><input type="text" size="6" maxlength=10" name="payment_deposit">
<input type="hidden" name="insert_values" value="1">
<input type="submit" value="Enter">
<td>
</tr>
</form>
<?php
// display array
$p = 0;
while ($p < $i){
?>
<?php
// switch account contents 1 = Wingfoot, 101 = Fuel, 102=service, 103=telephone
switch ($account_[$p]){
case "1":
$account_text = "Payment";
break;
case "101":
$account_text = "Fuel";
break;
case "102":
$account_text = "Service";
break;
case "103":
$account_text = "Telephone";
break;
}
?>
<tr>
<td><?php echo $item_[$p]; ?>
<td><?php echo $date_[$p]; ?>
<td><?php echo $description_[$p]; ?>
<td><?php echo $account_text; ?>
<td><?php echo $memo_[$p]; ?>
<!-- comment out <td align="right"><?php echo $payment_deposit_[$p]; ?> -->
<td align="right"><?php printf("%.2f" , $payment_deposit_[$p]); ?>
<td>
<?php
// confirm section
// if $confirm[$p] = "1" then display "*"
// else input button "confirm"
if($confirm_[$p] == 1){
?>
*
<?php
} else { ?>
<form style="margin:0;" method="post" action="<?php $_SERVER[PHP_SELF]; ?>">
<input type="hidden" name="item_number" value="<?php echo $item_[$p]; ?>">
<input type="hidden" name="confirm_" value="1">
<input type="submit" value="Confirm">
</form>
<?php } ?>
</tr>
<?php
$p++;
}
// reset index counters to 0
$p = 0;
$i = 0;
?>
</table>[/code]
[code=php]$pmt = $thousand - $subtotal;[/code]
Please see my code below to echo all 3 variables so we can see those values, specifically. All the other code doesn't seem relevant to the issue at hand. I did check out the debugging attachment and the numbers are floated properly, some positive and some negative.[code=php]$q = 0;
settype($subtotal, 'float');
while ($q < $i){
settype($payment_deposit_[$q], 'float');
$subtotal += $payment_deposit_[$q];
$q++;
}
settype($thousand, 'float');
$thousand=1000;
settype($pmt, 'float');
$pmt = $thousand - $subtotal;
echo "<br />Payment: $pmt $thousand=$thousand $subtotal=$subtotal";
[/code]
...
[code=php]$q = 0;
settype($subtotal, 'float');
while ($q < $i){
settype($payment_deposit_[$q], 'float');
$subtotal += $payment_deposit_[$q];
$q++;
}
settype($thousand, 'float');
$thousand=1000;
settype($pmt, 'float');
$pmt = $thousand - $subtotal;
echo "<br />Payment: $pmt $thousand=$thousand $subtotal=$subtotal";
[/code][COLOR=#000000] [/QUOTE][COLOR=#007700] [/COLOR]
-jim
[/COLOR]
Warning
Floating point precision
It is typical that simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9.
This is due to the fact that it is impossible to express some fractions in decimal notation with a finite number of digits. For instance, 1/3 in decimal form becomes 0.3.
So never trust floating number results to the last digit, and never compare floating point numbers for equality. If higher precision is necessary, thearbitrary precision math functions andgmp functions are available.[/quote]
Most likely you are running into floating point precision issues. Fromhttp://www.php.net/manual/en/language.types.float.php#language.types.float.casting :
-4.54747350886E-13 is 0.00000000000045...., which is very, very close to zero for most purposes. You may simply need to apply round() to such values to the desired degree of precision.[/QUOTE]
Excellent. In my mind to see the problem much more clearly I'm glad we took the time to change the code and properly debug all 3 key values. It just confirms what you originally said, lacking verified numbers and actual code at the time (plus changing to float). Funny, it's pretty darned obvious now!
Thanks, NogDog for assisting.
-jim[/QUOTE]
0.1.9 — BETA 5.21