1

I have a problem with this function:

function encludean_method($lat1,$lon1,$lat2,$lon2)
{
    $x1 = $lat1;
    $y1 = $lon1;
    $x2 = $lat2;
    $y2 = $lon2;
    $x = ( pow($x2,2) - pow($x1,2));
    $y = ( pow($y2,2) - pow($y1,2));
    $distance = (sqrt($x + $y));
    return $distance;
}

The problem when I call the above function with some values such as

(1.57454123333,103.6200516333,1.57483825,103.619484475)

it returns NaN. Can anyone tell me why that NaN is returned and how to solve it?

makes
  • 6,438
  • 3
  • 40
  • 58
Alsaket
  • 175
  • 1
  • 11

4 Answers4

7

$x and $y can assume negative values (which in fact does happen in your example), yielding in total a negative expression for $x + $y. I'm not sure whether what you're doing is right at all, wouldn't the correct version be more like:

$x1 = $lat1;
$y1 = $lon1;
$x2 = $lat2;
$y2 = $lon2;
$x = $x2 - $x1;
$y = $y2 - $y1;
$distance = (sqrt(pow($x, 2) + pow($y, 2)));
return $distance;

By squaring before calculating the square root, you're guaranteed to operate on positive values only.

misberner
  • 3,488
  • 20
  • 17
1

Try this

function euclidean_method($lat1,$lon1,$lat2,$lon2) {
    $x = $lat1-$lat2;
    $y = $lon1-$lon2;
    return sqrt($x*$x+$y*$y)
}
Michael Mior
  • 28,107
  • 9
  • 89
  • 113
  • Thank you so much Michael. I appreciate your reply so much – Alsaket Nov 08 '11 at 15:34
  • No problem. Just a general note that `pow` or it's equivalent in any language is typically less efficient than just performing the multiplication when it comes to squaring. (i.e. `$x*$x` will run faster than `pow($x,2)`) – Michael Mior Nov 08 '11 at 15:37
  • that's really nice to know. thanx again michael .. can I ask you one more question ? Do you have any idea how to convert the distance (returned value) into meters ? The parameters sent to the function are act the coordinates of two very near positions ... – Alsaket Nov 08 '11 at 15:53
0

The problem is that your x value is smaller than the negativity of your y and thus it will become a negative value you're trying to take the root of.

Marcus
  • 12,296
  • 5
  • 48
  • 66
0

Because of a wrong algorithm.

function encludean_method($lat1,$lon1,$lat2,$lon2)
{
    $x1 = doubleval($lat1);
    $y1 = doubleval($lon1);
    $x2 = doubleval($lat2);
    $y2 = doubleval($lon2);

    $x = doubleval( pow($x2 - $x1, 2.0));
    $y = doubleval( pow($y2 - $y1, 2.0));

    $distance = doubleval(sqrt($x + $y));

    return ($distance);
}

Note: (x12 - x22) < (x1 - x2)2.

That's why your code gave x = float(0.00093541820670495) and y = float(-0.11753762299304). So, sqrt(x + y) is too close to zero.

But if you are trying to get the distance between that point over the Earth surface, you should use another formula - the one you are using is applicable for Cartesian coordinate system only.

Notice, that latitude and longitude are measured in degrees. So the number you get is a bit misterious =)

So, after googling a bit, i've noticed some interesting services and formulas. Here's the code you are possibly trying to invoke:

function encludean_method($lat1, $lon1, $lat2, $lon2)
{
    $R = 6372.8; // km

    $lat1 = deg2rad($lat1);
    $lat2 = deg2rad($lat2);
    $lon1 = deg2rad($lon1);
    $lon2 = deg2rad($lon2);

    $a = (cos($lat2) * cos($lon2)) - (cos($lat1) * cos($lon1));
    $b = (cos($lat2) * sin($lon2)) - (cos($lat1) * sin($lon1));
    $c = sin($lat2) - sin($lat1);
    $ch = sqrt(pow($a, 2.0) + pow($b, 2.0) + pow($c, 2.0));
    $phi = 2.0 * asin($ch / 2.0);

    return $R * $phi;
}

Now, encludean_method(1.5745, 103.6200, 1.5748, 103.6195) returns 0.0648375378577 value, which is possibly the distance you are searching for; measured in kilometers.

Here's the example and one more (based on this answer). And here is the service i used to verify that code.

Hope this will help you! =)

Community
  • 1
  • 1
shybovycha
  • 11,556
  • 6
  • 52
  • 82
  • thank you so much shybovycha. it works well now. can i ask you one more question ? Actually the twos values sent to the function are (longitude and latitude ) to calculate the distance between these very near positions . what do you think the measure unit of that distance(the returned value) ? (miles, kilo, or meters ) ?? – Alsaket Nov 08 '11 at 15:37