Jump to content

Warning: Division by zero

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
Good day I got a warning like this :
Warning: Division by zero in C:\Documents and Settings\LT\report.php on line 122

Warning: Division by zero in C:\Documents and Settings\LT\report.php on line 220

I have this code in line 22:

$yield = ($c_output / $f_input) * 100;


and this is my code in line 220

$yield = ($sol_output / $buff_input) * 100;


my c_output is = 65.17
f_input is = 68.40

so the result should be : 95.27 but on my output is 0.00%

and my sol_output = 0.00
buff_input = 0. 00

and the result is 0.00 % which is correct but why I got this warning.


Thank you

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You will want to check basic things such as if the divisor is not zero before dividing, otherwise your application may be fraught with errors down the road.

I would do checks on each input:
if($f_input != 0) {
    $yield = ($c_output / $f_input) * 100;  
} else {
    $yield = 0;
}
The last code can work with negative numbers as well.

You also can print out each variable to ensure it really is not zero:
var_dump($c_output); //var_dump will display something like float(65.17)
var_dump($f_input); //so you know they are not zero
if($f_input != 0) {
    $yield = ($c_output / $f_input) * 100;  
} else {
    $yield = 0;
}
It is best to debug all possible variables when there is an error (or warning), especially if you know it's not 0 but it says it is, there could be a problem with your math before line 122!

Edited by Alexander, 22 November 2010 - 02:41 AM.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
thank you..

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts

newphpcoder said:

thank you..
I updated my last post, sorry if I was unclear.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.