试图在 JS 中编写此 excel 函数,但在模数部分遇到问题

Trying to write this excel function in JS but stump on the modulus portion

这是 excel 函数,它在 excel 中运行良好。

lng = mod(lng1-asin(sin(tc1)*sin(d)/cos(lat))+pi,2*pi)-pi

我正在尝试将其转换为 JavaScript,目前我有这个,但它不起作用。

    x = lng1-Math.asin(Math.sin(tc1)*Math.sin(d));
    y = Math.asin(Math.cos(lat)+Math.PI,2*Math.PI);
    n = (x % y)-Math.PI ;

我以弧度为单位传递坐标(并验证它们是正确的,如下例所示)但无法弄清楚为什么它给我 NaN

TIA 寻求帮助

史蒂夫

这是工作示例(从这里 https://edwilliams.org/avform.htm#Intermediate

         = mod(2.066470- asin(sin(1.150035)*sin(0.0290888)/cos(0.604180))+pi,2*pi)-pi
         = mod(2.034206+pi,2*pi)-pi radians
         = 2.034206 radians
         = 116 degrees 33min```

您在转换过程中犯了一些小的转换错误。为了让代码正常工作,下面是我的解决方案:

const result = lng1 - Math.asin((Math.sin(tc1) * Math.sin(d)) / Math.cos(lat)) +  Math.PI % (2 * Math.PI) - Math.PI;

解决方案可能看起来有点长,但它正常运行并按准确顺序完成操作。

鉴于:

let lng1 = 2.06647;
let tc1 = 1.150035;
let d = 0.0290888;
let lat = 0.60418;

代码将执行,名为 result 的最终常量的值为 2.0342057088546213,或大约 2.034206, 弧度 。如果需要转换为度数,只需将结果乘以因数 57.295779513082,因为 1 弧度等于 57.295779513082 度.