Dec 18 2011 | 10:59 am

I want to calculate distance with gps. I moved code from processing (java) to max patches.
Problem is that in processing i have those values for example:
dLat: -1.3315805E-7
dLon: 1.7476995E-7

which is like 0.00000017476995 if i'm correct.

In max the value is zero, and so after a few calculations i get nan (not a number).
What can i do?

:(

In case someone know's processing, here is the proceesing code.

/*
//pc
testP: gps 5.910325 30. 51.988632 57. 28.372467

//keuken
testP: gps 5.91006 10. 51.988777 19. 29.984245
*/

void setup() {
// println(getDistance(51.988632, 5.910325, 51.988777, 5.91006)); // pc > keuken
// println(getDistance(51.988777, 5.910006, 51.988739, 5.910056));
/*
String[] lines = loadStrings("geo.tsv");

String[] tokens = split(lines[0], "t");
float lon1 = float(tokens[2]);
float lat1 = float(tokens[4]);

for (int i = 1; i < 2; i++) { //lines.length-1

tokens = split(lines[i+1], "t");
float lon2 = float(tokens[2]);
float lat2 = float(tokens[4]);

//println(lat2+"t"+lon2);
float d = getDistance(lon1, lat1, lon2, lat2);
println(d*1000);
}
*/
float d = getDistance(5.910161, 51.988705, 5.910171, 51.988697);
println(d*1000);

}

float getDistance(float lon1, float lat1, float lon2, float lat2) {
int R = 6371; // Radius of the earth in km
float dLat = radians(lat2-lat1);
float dLon = radians(lon2-lon1);

println("dLat: "+dLat+"ndLon: "+dLon);

float a = sin(dLat/2) * sin(dLat/2) + cos(radians(lat1)) * cos(radians(lat2)) * sin(dLon/2) * sin(dLon/2);
float c = 2 * atan2(sqrt(a), sqrt(1-a));
float d = R * c; // Distance in km

return d;
}