# setting precision for float var

hello i got a bit of code calculating distance between 2 points now i know that the formula uses only the first 3 numbers of the lat and long also in the formula itself is it only using the first 3 numbers :S i would like to know a way to set the precision or something so i can get the formula to work. thank you verry much.

``````int distc(float Lattarget, float Lontarget, float Latloc, float Lonloc){ // latT lonT latL lonL
r = 6371;
Serial.println(Lattarget, 20); // here i see 20 decimals as needed
Serial.println(Lontarget); // here i don't ( that is logically ) but the //formula neets to put out around 8000 it doesnt now :( so i think it //uses only the first 3 numbers :S
Serial.println(Latloc);
Serial.println(Lonloc);
dlat = (Lattarget-Latloc);
dlon = (Lontarget-Lonloc);
A = (sin(dlat/2)) * (sin(dlat/2)) + (cos(Latloc)) * (cos(Lattarget)) * (sin(dlon/2)) * (sin(dlon/2));
C = 2 * (atan2(sqrt(A), sqrt(1-A)));
D = r * C;
lcd.setCursor(5,3);
lcd.print(D);
return D;
``````

thank you verry much for your time :D

here i see 20 decimals as needed

Your comment may be misleading you - you may see 20 digits, but your "float" is only accurate to about 6 decimal places.

aha so if i write ie 53.123456789 is a float the 789 may be different since the float variable is only precise for 6 decimals.? if i make them all a double then it will work better and preciser since then it will be double and precise for 12 decimals.

thank you :D

if i make them all a double then it will work better

That's the theory, but on Arduino, "float" is the same as "double".

Are you working with radians or degress??

That is C trig functions expect radians. If you pass degrees as arguments you're not likely to get any result that makes sense irrespective of precision.

i get degrees adn decimal minutes from my gps the i convert them to degrees(i convert them i am not cutting the minutes off) then i convert them to decimals

latXXX = (latD + (latX / 60) + ( (latXX / 10000)/ 60)); // to degrees lonXXX = (lonD + (lonX / 60) + ( (lonXX / 10000)/ 60)); // to degrees it is a bit wierd i know but the latD is the degrees the lat X is the decimal minutes(whole minutes) // and the latXX is the deciamal minutes ( wich are not whole minutes)

lat = ((latXXX * PI) / 180); // to decimal lon = ((lonXXX * PI) / 180); // to decimal

Useful defines in wiring.h:

``````#define radians(deg) ((deg)*DEG_TO_RAD)
``````

...so i can get the formula to work.

If you're still struggling, perhaps you wantn to share two sets of coordinates and show us what the output (distance) is? It should be possible then to see if your issue is down to rounding, implementation or other.

``````double targetlat = 53.22015354288012;
double targetlon = 6.572809517383575;

// first get the numbers from the array and store them in my own array ( yes it is kind of a detour
// but still it is easyer this way i think since i was getting errors i could not transform
// the variables and stuff..
// everything in the GPS.arguments sentence is exploded so GPS.arguments is the first char wich is 5 and so on
// GPS.arguments contains: 5317.7652
//GPS.arguments contains: 00635.9691

// lat deg
tijdelijk = GPS.arguments;
tijdelijk = GPS.arguments;

//lon deg
tijdelijk = GPS.arguments;
tijdelijk = GPS.arguments;
tijdelijk = GPS.arguments;

// decimal minutes of lat
tijdelijk = GPS.arguments;
tijdelijk = GPS.arguments;

// the rest of the decimal lat minutes (the precise kind:P)
tijdelijk = GPS.arguments;
tijdelijk = GPS.arguments;
tijdelijk = GPS.arguments;
tijdelijk = GPS.arguments;

// decimal minutes of lon
tijdelijk = GPS.arguments;
tijdelijk = GPS.arguments;

// the rest of the decimal lon minutes
tijdelijk = GPS.arguments;
tijdelijk = GPS.arguments;
tijdelijk = GPS.arguments;
tijdelijk = GPS.arguments;

// then store my array in floats latD is degrees latX is whole minutes
// latXX is decimal minutes and latXX is the degrees form then the lat form is the radial form wich is needed for the distance
latD = atol(tijdelijk);
lonD = atol(tijdelijk);
latX = atoi(tijdelijk);
latXX =atoi(tijdelijk);
lonX = atoi(tijdelijk);
lonXX =atoi(tijdelijk);
latXXX =  (latD + (latX / 60) + ( (latXX / 10000)/ 60)); // to degrees
lonXXX =  (lonD + (lonX / 60) + ( (lonXX / 10000)/ 60)); // to degrees
lat = ((latXXX * PI) / 180); // to radials
lon = ((lonXXX * PI) / 180); // to radials

int distc(float Lattarget, float Lontarget, float Latloc, float Lonloc){ // latT lonT latL lonL
r = 6371;
Serial.println(Lattarget);
Serial.println(Lontarget);
Serial.println(Latloc);
Serial.println(Lonloc);
dlat = (Lattarget-Latloc);
dlon = (Lontarget-Lonloc);
A = (sin(dlat/2)) * (sin(dlat/2)) + (cos(Latloc)) * (cos(Lattarget)) * (sin(dlon/2)) * (sin(dlon/2));
C = 2 * (atan2(sqrt(A), sqrt(1-A)));
D = r * C;
lcd.setCursor(5,3);
lcd.print(D);
return D;

}

distc(targetlat, targetlon, lat, lon);
``````

and eventiually it puts out 12834.72 and that is in meters but i know the distance between me and my school is 8 km ( in a straight line)

thank you verry much :)

im sorry the starting point is as my gps puts it out in the GPS.arguments string in the comments i pasted : // GPS.arguments contains: 5317.7652 //GPS.arguments contains: 00635.9691

then the values are exploded in the GPS.arguments string and then written in my string then they are written in floats using atol then the degrees are calculated then the radials are calculated then the radials are put in the calculator

writing this i know what i did wrong i put degrees into my distance calculator from the target point not radials.... ::)

double targetlat = 53.22015354288012; double targetlon = 6.572809517383575;

I assume above is the target. Do you have a starting point as well so we can calculate the distance?

yep putting in double targetlat = 0.92886690774018328978495972063534; double targetlon = 0.11471716718476285341072343233356;

as target has done the trick :3 thank you all verry much:D

If you want help on this, it would be better if you posted you sketch, including variable declarations.

You don't say if your geodetic systems are identical for both locations, though a 4km error is excessive. Posts crossed in the ether.[/edit]

as target has done the trick :3 thank you all verry much:D

So forgetting to convert the target coordinates to radians was the issue then. Good to see you got it working. :)