无法在javascript和java之间移植数学代码

时间:2013-07-03 13:31:44

标签: java javascript maps

我无法移植在“纬度/经度”和“经度”之间转换的JavaScript代码。 OS National Grid参考点,Java。 (http://www.movable-type.co.uk/scripts/latlong-gridref.html

我在一些数学运算中得到了不同的结果。 我在下面添加了 javascript java 代码,但在{em> javascript Ma的{​​{1}}计算结果会发生什么>和{em> java 中的0.04195508514183418。我打印了计算的输入,它们是相同的。

这是 javascript 代码:

0.04195511450680837

这是输出:

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 = (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);

这是java代码:

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

这是输出:

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 = (a-b)/(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

1 个答案:

答案 0 :(得分:5)

5/4将被解释为整数除法,因此会产生5 / 4 = 1.25 = 1

将其更改为5.0 / 4,因此它将使用浮点类型而不是整数。多次计算Ma,Mb,Mc和Md就属于这种情况。

也许这已经使你的结果变得均匀,但就数学本身应该起作用而言,它肯定会使它们正确。我认为除此之外你还正确地迁移了代码。