简体   繁体   English

Java Math库的Objective-C等效项是什么?

[英]What is the Objective-C equivalent of the Java Math library?

What library/header/class is equivalent to the Java Math class? 哪个库/标头/类与Java Math类等效?


Background & bonus question: 背景和奖金问题:

I'm trying to port this function to Objective-C from Java. 我正在尝试将此功能从Java移植到Objective-C。 Should I rewrite it, or can I copy and paste and rewrite only the parts that are syntactically different? 我应该重写它,还是只能复制和粘贴并重写语法上不同的部分? (In other words, will the Java behave the same way if it were run as Objective-C or C?) (换句话说,如果将Java作为Objective-C或C运行,Java的行为方式是否相同?)

Here's the monster function in Java. 这是Java中的Monster函数。 It essentially is the Vincinty Formula : 本质上是Vincinty公式

private double vincentyFormula(GeoLocation location, int formula) {
    double a = 6378137;
    double b = 6356752.3142;
    double f = 1 / 298.257223563; // WGS-84 ellipsiod
    double L = Math.toRadians(location.getLongitude() - getLongitude());
    double U1 = Math
            .atan((1 - f) * Math.tan(Math.toRadians(getLatitude())));
    double U2 = Math.atan((1 - f)
            * Math.tan(Math.toRadians(location.getLatitude())));
    double sinU1 = Math.sin(U1), cosU1 = Math.cos(U1);
    double sinU2 = Math.sin(U2), cosU2 = Math.cos(U2);

    double lambda = L;
    double lambdaP = 2 * Math.PI;
    double iterLimit = 20;
    double sinLambda = 0;
    double cosLambda = 0;
    double sinSigma = 0;
    double cosSigma = 0;
    double sigma = 0;
    double sinAlpha = 0;
    double cosSqAlpha = 0;
    double cos2SigmaM = 0;
    double C;
    while (Math.abs(lambda - lambdaP) > 1e-12 && --iterLimit > 0) {
        sinLambda = Math.sin(lambda);
        cosLambda = Math.cos(lambda);
        sinSigma = Math.sqrt((cosU2 * sinLambda) * (cosU2 * sinLambda)
                + (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda)
                * (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda));
        if (sinSigma == 0)
            return 0; // co-incident points
        cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;
        sigma = Math.atan2(sinSigma, cosSigma);
        sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
        cosSqAlpha = 1 - sinAlpha * sinAlpha;
        cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;
        if (Double.isNaN(cos2SigmaM))
            cos2SigmaM = 0; // equatorial line: cosSqAlpha=0 (ß6)
        C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
        lambdaP = lambda;
        lambda = L
                + (1 - C)
                * f
                * sinAlpha
                * (sigma + C
                        * sinSigma
                        * (cos2SigmaM + C * cosSigma
                                * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
    }
    if (iterLimit == 0)
        return Double.NaN; // formula failed to converge

    double uSq = cosSqAlpha * (a * a - b * b) / (b * b);
    double A = 1 + uSq / 16384
            * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
    double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
    double deltaSigma = B
            * sinSigma
            * (cos2SigmaM + B
                    / 4
                    * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B
                            / 6 * cos2SigmaM
                            * (-3 + 4 * sinSigma * sinSigma)
                            * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
    double distance = b * A * (sigma - deltaSigma);

    // initial bearing
    double fwdAz = Math.toDegrees(Math.atan2(cosU2 * sinLambda, cosU1
            * sinU2 - sinU1 * cosU2 * cosLambda));
    // final bearing
    double revAz = Math.toDegrees(Math.atan2(cosU1 * sinLambda, -sinU1
            * cosU2 + cosU1 * sinU2 * cosLambda));
    if (formula == DISTANCE) {
        return distance;
    } else if (formula == INITIAL_BEARING) {
        return fwdAz;
    } else if (formula == FINAL_BEARING) {
        return revAz;
    } else { // should never happpen
        return Double.NaN;
    }
  }

Java的Math类很大程度上基于标准C math.h ,因此您可以使用该类。

Looks like you're reimplementing existing code. 看起来您正在重新实现现有代码。 See MKMetersBetweenMapPoints() . 请参见MKMetersBetweenMapPoints()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM