Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Warning: Division by Zero error

    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!

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    It should be a problem with one of these

    PHP Code:
    $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)
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Something like that, but still if the value is 0 it's not going to work.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,302
    Thanks
    13
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by masterofollies View Post
    It should be a problem with one of these

    PHP Code:
    $castr_right = ($OldWidth-$OldWidth_castr)/2
    always solvable.

    Quote Originally Posted by masterofollies View Post
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Quote Originally Posted by Dormilich View Post
    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.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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).
    PHP Code:
    $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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    The output of that code:

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

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,302
    Thanks
    13
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by masterofollies View Post
    Fou lu kind of already corrected it, please read the posts above.
    double reminder better sinks in.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by MattF View Post
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Yup. Copied your code and the output verbatim.


  •  

    Posting Permissions

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