在javascript和java之间移植数学代码时出现问题

2022-02-25 00:00:00 javascript maps java

我在将在纬度/经度和amp;OS国家网格参考点之间进行转换的javascript代码移植到Java时遇到了问题。(http://www.movable-type.co.uk/scripts/latlong-gridref.html)

我在某些数学运算中得到了不同的结果。 我在下面包含了javascript和java代码,但是发生的是javascript中的0.04195508514183418和java中的0.04195511450680837计算结果。我已打印出计算的输入,它们是相同的。

这是javascript代码:

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

这是输出:

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

下面是Java代码:

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

这是输出:

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

解决方案

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

将其更改为5.0 / 4,以便它将使用浮点类型而不是整数。在多次计算mA、mb、mc和md时就是这种情况。

也许这已经使您的结果持平,但它肯定会使它们在数学本身应该起作用的情况下是正确的。除此之外,我认为您正确地迁移了代码。

相关文章