Class GeoMath


  • public class GeoMath
    extends Object
    Mathematical functions needed by GeographicLib.

    Define mathematical functions and constants so that any version of Java can be used.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int digits
      The number of binary digits in the fraction of a double precision number (equivalent to C++'s numeric_limits<double>::digits).
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void AngDiff​(Pair p, double x, double y)
      The exact difference of two angles reduced to [−180°, 180°].
      static double AngNormalize​(double x)
      Normalize an angle.
      static double AngRound​(double x)
      Coarsen a value close to zero.
      static double atan2d​(double y, double x)
      Evaluate the atan2 function with the result in degrees
      static double atanh​(double x)
      The inverse hyperbolic tangent function.
      static double LatFix​(double x)
      Normalize a latitude.
      static void norm​(Pair p, double sinx, double cosx)
      Normalize a sine cosine pair.
      static double polyval​(int N, double[] p, int s, double x)
      Evaluate a polynomial.
      static void sincosd​(Pair p, double x)
      Evaluate the sine and cosine function with the argument in degrees
      static void sincosde​(Pair p, double x, double t)
      Evaluate the sine and cosine function with reduced argument plus correction
      static double sq​(double x)
      Square a number.
      static void sum​(Pair p, double u, double v)
      The error-free sum of two numbers.
    • Field Detail

      • digits

        public static final int digits
        The number of binary digits in the fraction of a double precision number (equivalent to C++'s numeric_limits<double>::digits).
        See Also:
        Constant Field Values
    • Method Detail

      • sq

        public static double sq​(double x)
        Square a number.

        Parameters:
        x - the argument.
        Returns:
        x2.
      • atanh

        public static double atanh​(double x)
        The inverse hyperbolic tangent function. This is defined in terms of Math.log1p(x) in order to maintain accuracy near x = 0. In addition, the odd parity of the function is enforced.

        Parameters:
        x - the argument.
        Returns:
        atanh(x).
      • norm

        public static void norm​(Pair p,
                                double sinx,
                                double cosx)
        Normalize a sine cosine pair.

        Parameters:
        p - return parameter for normalized quantities with sinx2 + cosx2 = 1.
        sinx - the sine.
        cosx - the cosine.
      • sum

        public static void sum​(Pair p,
                               double u,
                               double v)
        The error-free sum of two numbers.

        Parameters:
        u - the first number in the sum.
        v - the second number in the sum.
        p - output Pair(s, t) with s = round(u + v) and t = u + v - s.

        See D. E. Knuth, TAOCP, Vol 2, 4.2.2, Theorem B.

      • polyval

        public static double polyval​(int N,
                                     double[] p,
                                     int s,
                                     double x)
        Evaluate a polynomial.

        Parameters:
        N - the order of the polynomial.
        p - the coefficient array (of size N + s + 1 or more).
        s - starting index for the array.
        x - the variable.
        Returns:
        the value of the polynomial.

        Evaluate y = ∑n=0..N ps+n xNn. Return 0 if N < 0. Return ps, if N = 0 (even if x is infinite or a nan). The evaluation uses Horner's method.

      • AngRound

        public static double AngRound​(double x)
        Coarsen a value close to zero.

        Parameters:
        x - the argument
        Returns:
        the coarsened value.

        This makes the smallest gap in x = 1/16 − nextafter(1/16, 0) = 1/257 for reals = 0.7 pm on the earth if x is an angle in degrees. (This is about 1000 times more resolution than we get with angles around 90 degrees.) We use this to avoid having to deal with near singular cases when x is non-zero but tiny (e.g., 10−200). This converts −0 to +0; however tiny negative numbers get converted to −0.

      • AngNormalize

        public static double AngNormalize​(double x)
        Normalize an angle.

        Parameters:
        x - the angle in degrees.
        Returns:
        the angle reduced to the range [−180°, 180°).

        The range of x is unrestricted.

      • LatFix

        public static double LatFix​(double x)
        Normalize a latitude.

        Parameters:
        x - the angle in degrees.
        Returns:
        x if it is in the range [−90°, 90°], otherwise return NaN.
      • AngDiff

        public static void AngDiff​(Pair p,
                                   double x,
                                   double y)
        The exact difference of two angles reduced to [−180°, 180°].

        Parameters:
        x - the first angle in degrees.
        y - the second angle in degrees.
        p - output Pair(d, e) with d being the rounded difference and e being the error.

        This computes z = yx exactly, reduced to [−180°, 180°]; and then sets z = d + e where d is the nearest representable number to z and e is the truncation error. If z = ±0° or ±180°, then the sign of d is given by the sign of yx. The maximum absolute value of e is 2−26 (for doubles).

      • sincosd

        public static void sincosd​(Pair p,
                                   double x)
        Evaluate the sine and cosine function with the argument in degrees
        Parameters:
        p - return Pair(s, t) with s = sin(x) and c = cos(x).
        x - in degrees.

        The results obey exactly the elementary properties of the trigonometric functions, e.g., sin 9° = cos 81° = − sin 123456789°.

      • sincosde

        public static void sincosde​(Pair p,
                                    double x,
                                    double t)
        Evaluate the sine and cosine function with reduced argument plus correction
        Parameters:
        p - return Pair(s, t) with s = sin(x + t) and c = cos(x + t).
        x - reduced angle in degrees.
        t - correction in degrees.

        This is a variant of GeoMath.sincosd allowing a correction to the angle to be supplied. x x must be in [−180°, 180°] and t is assumed to be a small correction. GeoMath.AngRound is applied to the reduced angle to prevent problems with x + t being extremely close but not exactly equal to one of the four cardinal directions.

      • atan2d

        public static double atan2d​(double y,
                                    double x)
        Evaluate the atan2 function with the result in degrees
        Parameters:
        y - the sine of the angle
        x - the cosine of the angle
        Returns:
        atan2(y, x) in degrees.

        The result is in the range [−180° 180°]. N.B., atan2d(±0, −1) = ±180°.