Author Topic: PHP + division by zero  (Read 3291 times)

Canuck

  • Eh?!!
  • Founders
  • Posts: 792
  • Karma: +51/-3
  • Andy Moog Fan
    • My Website
PHP + division by zero
« on: September 07, 2006, 08:26:26 PM »
Hey,

Im doing some calculations for my updated Andy Moog site.. (it will be updated soon.  Ill have a new thread for it :)).

Im calculating how many team issued items I need, which is 0/23 and returns 0.00%, the problem is I get a warning saying division by zero.  That is the correct value, but how can I supress the warning message?

Thanks


hans

  • Guitar Addict
  • Jackass In Charge
  • Posts: 3523
  • Karma: +46/-18
Re: PHP + division by zero
« Reply #1 on: September 07, 2006, 10:16:01 PM »
Division by zero errors should only occur if your denominator is 0 (like 0/0), you should be OK with 0/23. Never done it in PHP but it works fine in all the other languages I know.

Got a code fragment?
This signature intentionally left blank.

Canuck

  • Eh?!!
  • Founders
  • Posts: 792
  • Karma: +51/-3
  • Andy Moog Fan
    • My Website
Re: PHP + division by zero
« Reply #2 on: September 07, 2006, 11:07:56 PM »
As I said, its weird, it will give me the output I want, yet it still complains..

Code: [Select]

$result = mysql_query("SELECT count(*) from collection where bool_coll = '0' AND bool_team_iss = '1'");
$total_team_items_coll =  mysql_result($result, 0, 0);

$result = mysql_query("SELECT count(*) from collection where bool_team_iss = '1'");
$total_team_items =  mysql_result($result, 0, 0);

$total_items_coll = $total_cards_coll + $total_team_items_coll;

$perc_team_items_coll = ($total_team_items/$total_team_items_coll)*100; (it complains about this)

printf("Percentage of collection wanted: (%.0f/%.0f) = %.2f%%", $total_items_coll, $total_items, $perc_total_items);

//Is it Opera, or is the code text really small?

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: PHP + division by zero
« Reply #3 on: September 08, 2006, 12:50:02 AM »
You are dividing by $total_team_items_coll to calculate $perc_team_items_coll, yet in the output you are saying you are dividing by $total_items which I don' even see defined.

On a side note you really should be calling mysql_free_result($result) after you are done with them.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP + division by zero
« Reply #4 on: September 08, 2006, 09:32:41 AM »
You could always do a simple if statement to check for the division by zero and just output 0 if that's the case.

Canuck

  • Eh?!!
  • Founders
  • Posts: 792
  • Karma: +51/-3
  • Andy Moog Fan
    • My Website
Re: PHP + division by zero
« Reply #5 on: September 09, 2006, 05:49:07 PM »
>>You are dividing by $total_team_items_coll to calculate $perc_team_items_coll, yet in the output you are saying you are dividing by $total_items which I don' even see defined.

I forgot to put it in, sorry.

As it turns out it was a division by zero, I thought I was doing 0/23, when in fact I was performing 23/0.. but thanks for the input.