I have a problem with porting javascript code that converts values between Latitude / Longitude and OS National Grid Reference points in Java. ( http://www.movable-type.co.uk/scripts/latlong-gridref.html )
I get different results in some mathematical operations. I have included javascript and java code below, but what happens is the results of calculating Ma in 0.04195508514183418 in javascript and 0.04195511450680837 in java. I printed the input for the calculation, and they are the same.
This is the javascript code:
OsGridRef.osGridToLatLong = function(gridref) { var E = gridref.easting; var N = gridref.northing; var a = 6377563.396, b = 6356256.910; // Airy 1830 major & minor semi-axes var F0 = 0.9996012717; // NatGrid scale factor on central meridian var lat0 = 49*Math.PI/180, lon0 = -2*Math.PI/180; // NatGrid true origin var N0 = -100000, E0 = 400000; // northing & easting of true origin, metres var e2 = 1 - (b*b)/(a*a); // eccentricity squared var n = (ab)/(a+b), n2 = n*n, n3 = n*n*n; var lat=lat0, M=0; var count = 0; do { count++; lat = (N-N0-M)/(a*F0) + lat; console.log("pre ma calc"); console.log("n = " + n); console.log("n2 = " + n2); console.log("n3 = " + n3); console.log("lat = " + lat); console.log("lat0 = " + lat0); var Ma = (1 + n + (5/4)*n2 + (5/4)*n3) * (lat-lat0); console.log("post ma calc ma = " + Ma);
This is the conclusion:
pre ma calc test.html:68 n = 0.0016732202503250534 test.html:69 n2 = 0.0000027996660060978346 test.html:70 n3 = 4.684457855549562e-9 test.html:71 lat = 0.8970962185213205 test.html:72 lat0 = 0.8552113334772214 test.html:73 post ma calc ma = 0.04195511450680837
And here is the Java code:
LatLon osGridToLatLong(OsGridRef osGridRef) { int E = osGridRef.easting; int N = osGridRef.northing; double a = 6377563.396, b = 6356256.910; // Airy 1830 major & minor semi-axes double F0 = 0.9996012717; // NatGrid scale factor on central meridian double lat0 = 49*Math.PI/180, lon0 = -2*Math.PI/180; // NatGrid true origin double N0 = -100000, E0 = 400000; // northing & easting of true origin, metres double e2 = 1 - (b*b)/(a*a); // eccentricity squared double n = (ab)/(a+b), n2 = n*n, n3 = n*n*n; double lat=lat0, M=0; int count = 0; do { count++; lat = (N-N0-M)/(a*F0) + lat; Log.e(TAG, "pre ma calc"); Log.e(TAG, "n = " + n); Log.e(TAG, "n2 = " + n2); Log.e(TAG, "n3 = " + n3); Log.e(TAG, "lat = " + lat); Log.e(TAG, "lat0 = " + lat0); double Ma = (1 + n + (5/4)*n2 + (5/4)*n3) * (lat-lat0); Log.e(TAG, "post ma calc ma = " + String.valueOf(Ma)); double Mb = (3*n + 3*n*n + (21/8)*n3) * Math.sin(lat-lat0) * Math.cos(lat+lat0); double Mc = ((15/8)*n2 + (15/8)*n3) * Math.sin(2*(lat-lat0)) * Math.cos(2*(lat+lat0)); double Md = (35/24)*n3 * Math.sin(3*(lat-lat0)) * Math.cos(3*(lat+lat0)); M = b * F0 * (Ma - Mb + Mc - Md); // meridional arc } while (N-N0-M >= 0.00001); // ie until < 0.01mm
This is the conclusion:
07-03 12:36:03.413: E/DSDS(779): pre ma calc 07-03 12:36:03.423: E/DSDS(779): n = 0.0016732202503250534 07-03 12:36:03.423: E/DSDS(779): n2 = 2.7996660060978346E-6 07-03 12:36:03.443: E/DSDS(779): n3 = 4.684457855549562E-9 07-03 12:36:03.473: E/DSDS(779): lat = 0.8970962185213205 07-03 12:36:03.473: E/DSDS(779): lat0 = 0.8552113334772214 07-03 12:36:03.473: E/DSDS(779): post ma calc ma = 0.04195508514183418