Having difficulty translating JavaScript code that converts between Latitude/Longitude & OS National Grid Reference points to Java. ()
Encountering variations in the results of certain mathematical operations. The javascript and java code snippets provided below show a discrepancy in the Ma
calculation output, with 0.04195508514183418
being the result in javascript, while 0.04195511450680837
is produced in Java. Despite confirming identical input values for the calculation.
This represents the JavaScript code:
OsGridRef.osGridToLatLong = function(gridref) {
var E = gridref.easting;
var N = gridref.northing;
var a = 6377563.396, b = 6356256.910;
var F0 = 0.9996012717;
var lat0 = 49*Math.PI/180, lon0 = -2*Math.PI/180;
var N0 = -100000, E0 = 400000;
var e2 = 1 - (b*b)/(a*a);
var n = (a-b)/(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);
Utilizing the following Javascript code produces this output:
pre ma calc test.html:68
n = 0.0016732202503250534
n2 = 0.0000027996660060978346
n3 = 4.684457855549562e-9
lat = 0.8970962185213205
lat0 = 0.8552113334772214
post ma calc ma = 0.04195511450680837
The corresponding java code snippet looks like this:
LatLon osGridToLatLong(OsGridRef osGridRef) {
int E = osGridRef.easting;
int N = osGridRef.northing;
double a = 6377563.396, b = 6356256.910;
double F0 = 0.9996012717;
double lat0 = 49*Math.PI/180, lon0 = -2*Math.PI/180;
double N0 = -100000, E0 = 400000;
double e2 = 1 - (b*b)/(a*a);
double n =(a-b)/(a+b), n2 = n*n, n3 = n*n*n;
...
} while (N-N0-M >= 0.00001);
Executing this Java code provides the subsequent output:
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