SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot jsk137's Avatar
    Join Date
    Jan 2002
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error: division by zero

    When this calculation is run, I get this:

    Warning: Division by zero in sql5.php on line 79

    line 79 is this one -

    $radDist = atan(-$X / sqrt(-$X * $X+1)) + 2 * atan(1);




    PHP Code:
     $latRad1 $lat1 pi 180;
     
    $lonRad1 $lon1 pi 180;
     
    $latRad2 $lat2 pi 180;
     
    $lonRad2 $lon2 pi 180;
     
    $lonRadDif abs($lonRad1-$lonRad2);
     
    $X=sin($latRad1)*sin($latRad2)+cos($latRad1)*cos($latRad2)*cos($lonRadDif);
     
    $radDist atan(-$X sqrt(-$X $X+1)) + atan(1);
     
    $distCalc=$radDist*3958.754
    Can you help me make this work properly? If you need the whole code, let me know and I'll post it.

  2. #2
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well division by zero is obviously a mathematical problem. If you insist on trying to do that, or if it's a possibility and you don't WANT the errors to show up, just suppress it with an @.

    That's probably not what you wanted to hear, but otherwise you need to ensure that you're not dividfing by 0... :\
    Aaron Brazell
    Technosailor



  3. #3
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What are the values for $latRad1, $lonRad1, $latRad2 and $lonRad2?

    Also, this looks like a longitude, latitude distance calculator. What happens when you feed London's Longitude value of 0 Deg, 0 Mins in? Won't this cause a divide by zero?

  4. #4
    SitePoint Zealot jsk137's Avatar
    Join Date
    Jan 2002
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    torrent,

    All of those are set from a db table for the US.

    I am working on posting a test page you can go to and see all the involved values and the calculations.

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd just make sure that the value isn't 0 first before doing the calculation.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


Bookmarks

Posting Permissions

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