SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Division by zero help needed

    Hello all.

    This code is throwing up errors:

    PHP Code:

    $av 
    = ($Row[tons])/($Row[legs]);
    $av number_format($av,2); 
    It throws an error when tons = 0 and legs = 0 (which sometimes,..has to be like that)

    Is there anything I can do to improve it?

    Thnx in advance
    Matt

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. Test for a 0 value in the denominator, then skip the math if the denominator is equal to 0.
    John

  3. #3
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can we see more of the code, or know what exactly this script is supposed to do? PHP will not do division by 0. Would this work?

    PHP Code:
    if ($Row[tons] == "0" || $Row[legs] == "0") {
      
    $av 0;
    }
    else {
      
    $av = ($Row[tons])/($Row[legs]); 


  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    You have to define a value which should be presented if $Rows[legs] is equal to zero. In the code below, I used 'n/a'.

    PHP Code:
    // if you expect negative numbers change == to <=
    if ($Row['legs'] == 0) {
       
    $av 'n/a';
    }
    else {
       
    $av $Row['tons'] / $Row['legs'];
       
    $av number_format($av,2); 


  5. #5
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Chris (and all others)

    All working now thanks with no errors being thrown up.

    Here's where it was being used if anyone was interested:

    PHP Code:
    $Query "SELECT * from $TableName Order by Points desc";
        
    $Result mysql_db_query($DBName$Query$Link);
        
    // create a table to hold the results

        
    print ("<table border=0 width=\"95%\" cellspacing=2 cellpadding=1 align=center>\n");
        print (
    "<tr align=center valign=top>\n");
        print (
    "<td align=center valign=top>&nbsp;</td>\n");
        print (
    "<td align=center valign=top><b>Pld</b></td>\n");
        print (
    "<td align=center valign=top><b>W</b></td>\n");
        print (
    "<td align=center valign=top><b>L</b></td>\n");
        print (
    "<td align=center valign=top><b>F</b></td>\n");
        print (
    "<td align=center valign=top><b>A</b></td>\n");
        print (
    "<td align=center valign=top><b>Pts</b></td>\n");
        print (
    "<td align=center valign=top><b>Tons</b></td>\n");
        print (
    "<td align=center valign=top><b>Legs</b></td>\n");
        print (
    "<td align=center valign=top><b>Ave</b></td>\n");
        print (
    "<td align=center valign=top><b>Pen</b></td>\n");
        print (
    "</tr>\n");
        print (
    "<tr>\n");
        print (
    "<td colspan=11><hr></td>\n");
        print (
    "</tr>\n");
        
    // fetch the results from the database

        
    while ($Row mysql_fetch_array($Result)) {
    if (
    $Row[legs]==0){
        
    $av'n/a';
    }else{
        
    $av = ($Row[tons])/($Row[legs]);
        
    $av number_format($av,2);
        }        
            print (
    "<tr align=center valign=top>\n");
            print (
    "<td align=center valign=top>$Row[name]</td>\n");
            print (
    "<td align=center valign=top>$Row[played]</td>\n");
            print (
    "<td align=center valign=top>$Row[won]</td>\n");
            print (
    "<td align=center valign=top>$Row[lost]</td>\n");
            print (
    "<td align=center valign=top>$Row[forr]</td>\n");
            print (
    "<td align=center valign=top>$Row[against]</td>\n");
            print (
    "<td align=center valign=top>$Row[points]</td>\n");
            print (
    "<td align=center valign=top>$Row[tons]</td>\n");
            print (
    "<td align=center valign=top>$Row[legs]</td>\n");
            print (
    "<td align=center valign=top>$av</td>\n");
            print (
    "<td align=center valign=top>$Row[penalty]</td>\n");
            print (
    "<td>&nbsp;</td>\n");
            print(
    "<td align=center valign=top>&nbsp;</td>\n");
            print (
    "</tr>\n");
            
            }
            
            
    mysql_close($Link);
            print (
    "</table>\n");
            
    ?> 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •