cast string to float in php
Results 1 to 3 of 3

Thread: cast string to float in php

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    19

    cast string to float in php

    Hi Everyone,

    I'm trying to use a math equation ( see bottom of post) to get the distance of two points. I am able to get the data from the user and the server. However, when I try to use the equation, NAN(not a number) is displayed. I tried to resolve this issue by casting the variables to (float) but this did not work either. I'm not sure what is off here. Can you take a look?

    PHP Code:
    // get user input
    $userLong=(float)$_POST['userLong']=-73.9599609375;

    $userLat=(float)$_POST['userLat']=40.8169272319157
    [/PHP]




    PHP Code:
    //data was supplied from the server in the while loop.  Use foreach loop to parse data for  getDistance function 

    foreach ($_SESSION['events'] as $event) {
       echo 
    $event["long".$i];
       
    $eventLong=$event["long".$i];
       
    $eventLat$event["lat".$i];

      
    $distance getDistance($userLat,$eventLat$userLong$eventLong);
                    
                    echo 
    $distance;


                }
    // end foreach 


    PHP Code:
    // for each loop supplied data use in equation below to get distance. 

    function getDistance($lat1,$lat2,$lon1,$lon2){
       
    $lat1 = (int) $lat1;
       
    $lat2 = (int) $lat2;
       
    $lon1 = (int) $lon1;
       
    $lon2 = (int) $lon2;

       
    $radians =6371//km
       
    $d=acos(sin($lat1) * sin($lat2) + cos($lat1)* cos($lat2)* cos($lon1-$lon2)* $radians);
    return 
    $d;

        } 

  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    19
    I changed int to float:

    PHP Code:
    function getDistance($lat1,$lat2,$lon1,$lon2){
       
    $lat1 = (float) $lat1;
       
    $lat2 = (float) $lat2;
       
    $lon1 = (float) $lon1;
       
    $lon2 = (float) $lon2;

       
    $radians =6371//km
       
    $d=acos(sin($lat1) * sin($lat2) + cos($lat1)* cos($lat2)* cos($lon1-$lon2)* $radians);
    return 
    $d;

        } 

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    19
    Found Carmon Santdiago? Waldo? Michael Westen? NO? But I found the answer!. php function acros() returns NAN if the number is between 1 and infinity. This function only works with fractions ( in this case decimals). I snooped around and found another function on this site http://www.zipcodeworld.com/samples/distance.php.html

    this works fine:

    PHP Code:
    $theta $lon1 $lon2;
      
    $dist sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
      
    $dist acos($dist);
      
    $dist rad2deg($dist);

    return 
    $dist

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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