...

View Full Version : Warning: Division by Zero error



jwp99
03-29-2010, 05:54 PM
Our website was working perfectly fine for the last 6 months, and then just this month the page where we upload staff information has stopped working. We keep receiving a: Division by Zero error. Here is the area of the code that it says the error is in.....

if ($NewHeight=='1' and $NewWidth!='1') {
$NewHeight = ceil(($OldHeight*$NewWidth)/$OldWidth);
}
elseif ($NewWidth=='1' and $NewHeight!='1') {
$NewWidth = ceil(($OldWidth*$NewHeight)/$OldHeight);
}
elseif ($NewHeight=='1' and $NewWidth=='1') {
return(false);
}

$OldHeight_castr = ceil(($OldWidth*$NewHeight)/$NewWidth);
$castr_bottom = ($OldHeight-$OldHeight_castr)/2;

$OldWidth_castr = ceil(($OldHeight*$NewWidth)/$NewHeight);
$castr_right = ($OldWidth-$OldWidth_castr)/2;



Any help would be appreciated! Thanks so much!

masterofollies
03-29-2010, 05:57 PM
It should be a problem with one of these


$castr_right = ($OldWidth-$OldWidth_castr)/2;

Division by zero means exactly what it says, so if the value is 0 and you are trying to divide by 2, that doesn't work.

I'd say there is an error in getting the value (width or height), or else it's one or the other (such as 100px height, 0px width)

Fou-Lu
03-29-2010, 07:05 PM
Other way around mate; you can divide 0 by anything, but you cannot divide anything by 0.
So the problem will be with one of these:
$NewHeight = ceil(($OldHeight*$NewWidth)/$OldWidth);
$NewWidth = ceil(($OldWidth*$NewHeight)/$OldHeight);
$OldHeight_castr = ceil(($OldWidth*$NewHeight)/$NewWidth);
$OldWidth_castr = ceil(($OldHeight*$NewWidth)/$NewHeight);

$OldWidth, $OldHeight, $NewWidth, or $NewHeight will be 0. Start debugging or add print statements to see which one(s) is/are zero.

masterofollies
03-29-2010, 08:04 PM
Something like that, but still if the value is 0 it's not going to work.

Dormilich
03-29-2010, 08:46 PM
It should be a problem with one of these


$castr_right = ($OldWidth-$OldWidth_castr)/2;

always solvable.


Division by zero means exactly what it says, so if the value is 0 and you are trying to divide by 2, that doesn't work.
may I remind you of basic math? zero divided by anything but zero always gives zero.

dividing by zero is: y = x/0; which is not declared.

masterofollies
03-29-2010, 09:06 PM
always solvable.


may I remind you of basic math? zero divided by anything but zero always gives zero.

dividing by zero is: y = x/0; which is not declared.

Fou lu kind of already corrected it, please read the posts above.

Fou-Lu
03-29-2010, 09:12 PM
It should be noted in PHP though that $x / 0 should actually work. Aside from triggering the error itself (which is just a warning mind you), it should return null if my assumption is correct. Attempting to use that in a numerical context will actually allow you to operate on it (or should if my initial assumptions are correct).


$x = 1;
$y = 0;
$z = $x / $y; // Warning: div..... $x = null

printf("\$z is: %d\n", $z); // $z is: 0
printf("++\$z is: %d\n", ++$z); // ++$z is: 1


Does that work out to one? Anyone that can test that? I assume that ++null = 1, though --null = well null. Hah.

MattF
03-29-2010, 09:22 PM
The output of that code:



PHP Warning: Division by zero in divisor.php on line 5
$z is: 0
++$z is: 0

Dormilich
03-29-2010, 09:23 PM
Fou lu kind of already corrected it, please read the posts above.
double reminder better sinks in.

Fou-Lu
03-29-2010, 09:31 PM
The output of that code:

Really? I'm a little suprised; the API states that null can be incremented to one (but not decremented, or I guess more accurately null is decremented to null). I'd have expected that ++$z would result in 1.

MattF
03-30-2010, 05:34 PM
Yup. Copied your code and the output verbatim.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum