So I'm coding a program (c++), and thing leads to thing, and I need to find out how many meters to a degree of longitude at a specific latitude to a very high precision. Optimally < 1 meter at all latitudes(The program should be able to use this to plot many points given in lat and lon that are sub meter apart.) pretty much anywhere where there would normally be people (this means everywhere but within a few degrees of the poles).
When researching I came across some formulas to do this. The one that was most commonly available to me produced distances that were ~50 meters off for every 15 degrees of lat compared to what Wikipedia (and other online calcs) said they should be. The second formula seems to be more accurate, but I have no idea WHY its more accurate then the first one.
code/formulas:
double MetersAtLat(double lat)
{
long double pi = 3.14159265358979323846;
long double a = 6378137.0; //equatorial radius, meters exactly
long double b = 6356752.3142;// polar radius, meters exactly
long double eccentricitySquared = ((a*a) - (b*b))/(a*a);//0.00669437999014?;
long double rlat = (lat) * (pi/180);
//method 1; off by ~ 50m for every 15 degrees away from equator. (for some reason)
long double answ = (pi * a * cos(rlat))/180 * sqrt (1 - eccentricitySquared * (sin(rlat)*sin(rlat)));
//method 2; off by ~0.5m? at the equator and gets more accurate the more northern lats given?.
long double tanPsi = (b * tan(rlat))/a;
long double psi = atan(tanPsi);
long double answ2 = (pi * a * cos(psi))/180;
return answ2;
}
Is the problem with method 1 that it really is that inaccurate or is it really an issue with my computer/compiler/language? and is the second method as accurate as it looks? (are the numbers it returns good?)
edit: I've been using a table on Wikipedia to verify the numbers. (https://en.wikipedia.org/wiki/Latitude)