help for use of variables in a calculation

Hello everyone, I'm doing a project with a GPS and I'm stuck on the calculation of the distance between two coordinates, probably because I'm wrong to use the types of data. I need help !

I.E. I have two cordinates as described below:
LatA 43.824254
LngA 10.477824
LatB 43.825185
LngB 10.482122

The formula I have to use is :
= 6372.795477598 * arccos (sen (LatA) * sen (LatB) + cos (LatA) * cos (LatB) * cos (LngA-LngB))
This is on Execel and it works fine, verified through Google Earth (in this example: 360m)
-I know the values must be converted from degrees to radians (radians = degrees * 3,14/180) -

The code in C++ I wrote is:
float LatA = 43.824254;
float LATB = 43.825185;
float LngA = 10.477824;
float LNGB = 10.482122;
Float Dist;

Dist = 6372.795477598 * ACOs (sin (latA * 3.14/180) * sin (LATB * 3.14/180) + cos (lata * 3.14/180) * cos (LATB * 3.14/180) * cos (Lnga * 3.14/180-LNGB * 3.14/180));

But that does not work... thank you in advance:)

Please, first get rid of the following confusions:

  1. Arccos and ACOs are the same. Is it arc cosine?
  2. Sen and sin are the same. Is it sine function?
  3. Lata and latA are the same. Is it latitude A
  4. The following formula misses the balancing parenthses:
    The formula I have to use is :
    = 6372.795477598 * Arccos (Sen (Lata) * Sen (LATB) + cos (lata) * cos (LATB) * cos (Lnga-LNGB)
  1. yes, the first is in Excel (Arccos - italian version) and the second is in C++
  2. yes, same function (sen is in Excel) sin in C++
  3. yes
  4. ok, sorry i wrong in copy and paste. Formula works fine in Excel..

Thanks for the quick reply.

1. I have reformatted your equation as follows, and I also got the wrong result (2.20). Unit?

Dist = 6372.795477598 * acos(sin (LatA * (3.14 / 180)) * sin (LATB * (3.14 / 180)) + cos (LatA * (3.14 / 180)) * cos (LATB * (3.14 / 180)) * cos (Lnga * (3.14 / 180) - LNGB * (3.14 / 180)));
  Serial.println(Dist, 2);

2. Then I have used a formula from this link, and I have got 345m which is very close to 360m.

3. Can you please, give a link of the formula of Step-1 that you have used?

thank you for yuor help!
the formula comes from: Calcolo della distanza e della direzione tra due punti del pianeta direttamante sulla mappa
sorry this is in italian. You have to look below "Calcolo della distanza tra due punti geografici ", formula is there!
I tried to use your link and it also works, the result is 0,36km that is the same.

Hi,
When you use the two co-ordinates, is one of the co-ordinates the current position of the GPS?
In other words are you trying to calculate the distance from where the GPS is to a set point?

Have you got a GPS and controller working at the moment?

There is a provision in the GPS library for you to get the GPS to calculate the distance for you if you input into a function the set point long and lat.

Tom.... :slight_smile:

yes I have a GPS working fine! I would like to give a coordinate and know how far it is the point from actual position.
I use TinyGPS library... i found what you say.. just need to try, thank you

http://arduiniana.org/libraries/tinygpsplus/

Pi is spelt "PI", not "3.14", in the Arduino enviroment.

Floats do not have enough precision for this. If you feel like learning then do some research with Google.

Or use the library.

MarkT:
Pi is spelt "PI", not "3.14", in the Arduino enviroment.

"M_PI" is more common and portable.

thank you all for helping me :slight_smile:

I tried a different equation I found online called the "Haversine Formula":

https://andrew.hedges.name/experiments/haversine/

and it seems to return a meaningful value for the coordinates you gave (0.36011km or 360.11m).

I ifdef'd out your coordinates and used some from different ones to correlate with the site and got a very close value (1112.1062km vs their value of 1112.142km.)

#if 0 //you original coords
float LatA = 43.824254; 
float LATB = 43.825185;
float LngA = 10.477824;
float LNGB = 10.482122;
#else //sanity check coords used at https://andrew.hedges.name/experiments/haversine/
float LatA = 38.898556; 
float LATB = 48.897147;
float LngA = -77.037852;
float LNGB = -77.043934;
#endif

void setup() 
{
  // put your setup code here, to run once:
    Serial.begin(9600);
    while(!Serial);
    
}

float DtoRadians( float angle )
{
    return( angle * PI/180.0 );    
}//Degrees to Radians

void loop() 
{
    float
        dlon,
        dlat,
        a, c;
    float
        rLatA,
        rLATB,
        rLngA,
        rLNGB,
        Dist;

    rLatA = DtoRadians(LatA); 
    rLATB = DtoRadians(LATB);
    rLngA = DtoRadians(LngA);
    rLNGB = DtoRadians(LNGB);

    //https://andrew.hedges.name/experiments/haversine/
    dlon = DtoRadians(LNGB - LngA); 
    dlat = DtoRadians(LATB - LatA); 
    a = pow((sin(dlat/2.0)),2) + cos(rLatA) * cos(rLATB) * pow((sin(dlon/2.0)),2); 
    c = 2.0 * atan2( sqrt(a), sqrt(1-a) ); 
    Dist = 6372.795477598 * c;//
    
    Serial.println(Dist, 5);
    delay(1000);
    
}

Float variables and calculations are good to only 6 decimal digits, which is not enough for accurate lat/lon distance calculations, and doubles are not supported by Arduino.

GPS libraries like NeoGPS get around this by using a number of tricks, such as using long integers to store coordinates in units of degrees10^6 or degrees10^7.

jremington:
doubles are not supported by Arduino.

Well they are ;), it's just that on Uno etc they're the same size as floats.

Notes and Warnings
Users who borrow code from other sources that includes double variables may wish to examine the code to see if the implied precision is different from that actually achieved on ATMEGA based Arduinos.

Don't be silly. It is ridiculous that Arduino (avr-gcc actually) even allows the double keyword to be used, as it is seriously misleading.

The Arduino forum is full of posts related to the above, with people asking why they get the "wrong answer".

jremington:
Float variables and calculations are good to only 6 decimal digits, which is not enough for accurate lat/lon distance calculations, and doubles are not supported by Arduino.

Double is supported by Arduino DUE.

Double is supported on most computers, which is why it is such a serious mistake that Arduino/avr-gcc allows usage of the double keyword.

I have wanted to say that ATSAM3X8E based Arduino DUE Board allows to have a 15-digit accuracy (tested) after the decimal point in the binary64 formatted floating point calculation through the declaration of double x;.