# Thread: Calling math and php gurus

1. ## Calling math and php gurus

Take the following function that I shamelessly stole from somewhere on google:

Code PHP:
```function distance(\$lat1, \$lon1, \$lat2, \$lon2, \$unit) {
\$theta = \$lon1 - \$lon2;
\$dist = acos(\$dist);
\$miles = \$dist * 60 * 1.1515;
\$unit = strtoupper(\$unit);

if (\$unit == "K") {
return (\$miles * 1.609344);
} else if (\$unit == "N") {
return (\$miles * 0.8684);
} else {
return \$miles;
}
}```

Appears to work great. Then I tried comparing the same long / lat to each other, and the result is not 0...

I'm not sure if this is a crazy happening of PHP and the way it stores the number or perhaps the math itself? I found another function that was written differently and the same thing happens.

2. Can you provide samples?

Here is what I did:
PHP Code:
``` // Lima 40.329796, -84.210205// Columbus 39.968701, -83.001709\$distance1 = distance(40.329796, -84.210205, 40.329796, -84.210205, 'N');\$distance2 = distance(40.329796, -84.210205, 40.329796, -84.210205, 'N');var_dump(\$distance1, \$distance2); // float(0) float(0) is the output// Lima 40.329796, -84.210205// Columbus 39.968701, -83.001709\$distance1 = distance(40.329796, -84.210205, 39.968701, -83.001709, 'N');\$distance2 = distance(40.329796, -84.210205, 39.968701, -83.001709, 'N');var_dump(\$distance1, \$distance2); // float(59.505373431747) float(59.505373431747) is the output  ```

3. Yes, that works great, try this:

echo distance(40.329796, -84.210205, 40.329796, -84.210205,'N');

4. I get an output of float(0) when I do a var_dump.
PHP Code:
``` <?phpfunction distance(\$lat1, \$lon1, \$lat2, \$lon2, \$unit) {    \$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);    \$miles = \$dist * 60 * 1.1515;    \$unit = strtoupper(\$unit);    if (\$unit == "K") {        return (\$miles * 1.609344);    } else if (\$unit == "N") {        return (\$miles * 0.8684);    } else {        return \$miles;    }}// Lima 40.329796, -84.210205// Columbus 39.968701, -83.001709\$distance1 = distance(40.329796, -84.210205, 39.968701, -83.001709, 'N');\$distance2 = distance(40.329796, -84.210205, 39.968701, -83.001709, 'N');\$distance3 = distance(40.329796, -84.210205, 40.329796, -84.210205,'N');var_dump(\$distance1, \$distance2, \$distance3);?> ```

5. That's right, it was only on a few examples was I seeing this happen. I'll try to find one of them.

6. The only thing i can think of is a rounding error in deg2rad? But... no, that should come up with the same number even if it rounds wrong...

7. Originally Posted by K. Wolfe
That's right, it was only on a few examples was I seeing this happen. I'll try to find one of them.
Please do, as I'm interested in tracking that down. I imagine it is a floating point scenario or something with precision but without a sample that produces it, it is hard for me to verify that.

8. Simplifying the math;
\$theta = 0;
cos(0) = 1;

becomes

or, because \$lat1 and \$lat2 are the same;

the values of deg2rad for the same value will be the same, so i'ma call it \$x

\$dist = sin(\$x)^2 + cos(\$x)^2

My geometry lessons tell me that sin(x)^2+cos(x)^2 = 1.

acos(1) = 0.

so \$dist should = 0.

Please do, as I'm interested in tracking that down. I imagine it is a floating point scenario or something with precision but without a sample that produces it, it is hard for me to verify that.
It was especially interesting because it would return the same incorrect response every time it occurred (5.xxxxxxxxxxxxxx) not sure of the decimals, but it was long and truncated as well.

#### Posting Permissions

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