Page 1 of 1
divison by 0
Posted: Mon Feb 09, 2004 2:28 pm
by John Cartwright
I have this equation
Code: Select all
<?php
$percentage = $de_aztec_b / $de_aztec_a * 100;
?>
but sometimes $de_aztec_b will be 0 so the division won't work ( I get an error message saying cannot divide by 0. But for this script to work I would need it to sometimes be 0.
My script is designed for getting the percentage to win on a record based on the results of their record history by getting those 2 variables ( aztec_b is amount played and aztec_a is the amount won ). So what if they are 0-1 as their record. Any ideas?
Posted: Mon Feb 09, 2004 2:37 pm
by Illusionist
so, if the record was 0 to 1 then it would be:
$percentage = 0 / .01
well... i dont get an error when trying to do that....
Posted: Mon Feb 09, 2004 2:44 pm
by John Cartwright
This is acting really weird.. I'll post again with an explanation again if I need any help :S
Posted: Mon Feb 09, 2004 2:51 pm
by m3rajk
check to see if the variable on the bottom is 0 before dividing. if it is zero, in most cases, switching it to a 1 will result in the desired result, thus consider what result will be attained when you divide any given number by 1 at that point? if you're sure you want the other number (the normal case) then swithc it to 1, thus resolving the issue
Posted: Mon Feb 09, 2004 2:53 pm
by John Cartwright
I have tried that.. but the problem is
let's say their record is 0-1... setting the 0 to a 1 will result in 100%

Posted: Mon Feb 09, 2004 3:05 pm
by John Cartwright
Let's see if we can figure out a better way at this.
This is some more code to give you the big picture.. i also got a few more questions. Forget about the 0 part for now. Note the percentage variable is only setup for $de_aztec and $de_dust in the if statments.
Code: Select all
<?php
$de_dust2_a = 0;
$de_aztec_a = 0;
$de_nuke_a = 0;
$de_train_a = 0;
$de_cpl_fire_a = 0;
$de_cpl_mill_a = 0;
$de_inferno_a = 0;
$de_cbble_a = 0;
$de_comrade_a = 0;
$unknown_a = 0;
$de_dust2_b = 0;
$de_aztec_b = 0;
$de_nuke_b = 0;
$de_train_b = 0;
$de_cpl_fire_b = 0;
$de_cpl_mill_b = 0;
$de_inferno_b = 0;
$de_cbble_b = 0;
$de_comrade_b = 0;
$unknown_b = 0;
$pastmap = ""; // so that the first map always gets outputted.
// Setting variables from the table
while ( $row = mysql_fetch_array($result) ) {
$date = $row["date"];
$outcome = $row["outcome"];
$league = $row["league"];
$map = $row["map"];
$opponent = $row["opponent"];
$league = $row["league"];
if ($map == "de_dust2") {
$de_dust2_a = $de_dust2_a + 1;
if ($outcome == "win"){
$de_dust2_b = $de_dust2_b + 1; }
$percentage= $de_dust2_b / $de_dust2_a * 100;
}elseif ($map == "de_aztec") {
$de_aztec_a = $de_aztec_a + 1;
if ($outcome == "win"){
$de_aztec_b = $de_aztec_b + 1; }
$percentage = $de_aztec_b / $de_aztec_a * 100;
}elseif ($map == "de_nuke") {
$de_nuke_a = $de_nuke_a + 1;
if ($outcome == "win"){
$de_nuke_b = $de_nuke_b + 1; }
$perc_de_nuke= $de_aztec_b / $de_nuke_a * 100;
}elseif ($map == "de_train") {
$de_train_a = $de_train_a + 1;
if ($outcome == "win"){
$de_train_b = $de_train_b + 1; }
}elseif ($map == "de_cpl_fire") {
$de_cpl_fire_a = $de_cpl_fire_a + 1;
if ($outcome == "win"){
$de_cpl_fire_b = $de_cpl_fire_b + 1; }
}elseif ($map == "de_cpl_mill") {
$de_cpl_mill_a = $de_cpl_mill_a + 1;
if ($outcome == "win"){
$de_cpl_mill_b = $de_cpl_mill_b + 1; }
}elseif ($map == "de_inferno") {
$de_inferno_a = $de_inferno_a + 1;
if ($outcome == "win"){
$de_inferno_b = $de_inferno_b + 1; }
}elseif ($map == "de_cbble") {
$de_cbble_a = $de_cbble_a + 1;
if ($outcome == "win"){
$de_cbble_b = $de_cbble_b + 1; }
}elseif ($map == "de_comrade") {
$de_comrade_a = $de_comrade_a + 1;
if ($outcome == "win"){
$de_comrade_b = $de_comrade_b + 1; }
}else{
$unknown = $unknown + 1;
if ($outcome == "win"){
$unknown_a = $unknown_b + 1; }
}
if ($pastmap != $map) {
echo "<tr>\n".
"<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
"<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n".
"<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$percentage</font></td>\n".
"<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2"></font></td>\n".
"</tr>\n";
}
$pastmap = $map; // set the current map to the past map.
///////wins/////////////loss///////////percentage
echo "$de_aztec_b - $de_aztec_a - $percentage";
?>
My concerns are right now that the percentages arn't being outputted correctly, it is showing you the record for $de_dust2 being 0-1-50% when I believe it should be 2-2 - 100%:S
I dunt know this is really weird. a link to this is found @
http://www.jcartonline.com/xe/?p=2&r=cal Look near the bottom where it sais "Some Stats"