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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Division by Zero Using Floating point. Why?

    Hi all,
    my code has the following equation and the number was gotton from the mysql database:

    $num = - 5.5813651706861/ 1.8673620948785

    But somehow I got the following errors:

    Warning: Division by zero in /home/mysite/public_html/myfile.php on line 564

    Do you know why?
    Thanks.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Something tells me the actual code you are running ends up with a zero in the denominator.

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    No, I echo the variables and the numbers I included in the original post is the value of the variables. It's not 0. It's a really long floating number.

    Quote Originally Posted by Fumigator View Post
    Something tells me the actual code you are running ends up with a zero in the denominator.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can prevent the error with an "if" statement.

    Also try casting the variables to float type.
    PHP Code:
    if ((float) $b == 0) {
        echo 
    "Attempt to divide by 0.";
        
    //handle the situation
    } else {
        
    $v = (float) $a / (float) $b;



  •  

    Posting Permissions

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