public final class MathUtils
extends java.lang.Object
Math.| Modifier and Type | Field and Description | 
|---|---|
| static double | EPSILONSmallest positive number such that 1 - EPSILON is not numerically equal to 1. | 
| static double | SAFE_MINSafe minimum, such that 1 / SAFE_MIN does not overflow. | 
| static double | TWO_PI2 π. | 
| Modifier and Type | Method and Description | 
|---|---|
| static int | addAndCheck(int x,
           int y)Add two integers, checking for overflow. | 
| static long | addAndCheck(long a,
           long b)Add two long integers, checking for overflow. | 
| static long | binomialCoefficient(int n,
                   int k)Returns an exact representation of the  Binomial
 Coefficient, " n choose k", the number ofk-element subsets that can be selected from ann-element set. | 
| static double | binomialCoefficientDouble(int n,
                         int k)Returns a  doublerepresentation of the  Binomial
 Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set. | 
| static double | binomialCoefficientLog(int n,
                      int k)Returns the natural  logof the  Binomial
 Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set. | 
| static void | checkOrder(double[] val,
          int dir,
          boolean strict)Checks that the given array is sorted. | 
| static int | compareTo(double x,
         double y,
         double eps)Compares two numbers given some amount of allowed error. | 
| static double | cosh(double x)Returns the 
 hyperbolic cosine of x. | 
| static double | distance(double[] p1,
        double[] p2)Calculates the L2 (Euclidean) distance between two points. | 
| static double | distance(int[] p1,
        int[] p2)Calculates the L2 (Euclidean) distance between two points. | 
| static double | distance1(double[] p1,
         double[] p2)Calculates the L1 (sum of abs) distance between two points. | 
| static int | distance1(int[] p1,
         int[] p2)Calculates the L1 (sum of abs) distance between two points. | 
| static double | distanceInf(double[] p1,
           double[] p2)Calculates the L∞ (max of abs) distance between two points. | 
| static int | distanceInf(int[] p1,
           int[] p2)Calculates the L∞ (max of abs) distance between two points. | 
| static boolean | equals(double[] x,
      double[] y)Returns true iff both arguments are null or have same dimensions
 and all their elements are  equals | 
| static boolean | equals(double x,
      double y)Returns true iff both arguments are NaN or neither is NaN and they are
 equal | 
| static boolean | equals(double x,
      double y,
      double eps)Returns true iff both arguments are equal or within the range of allowed
 error (inclusive). | 
| static boolean | equals(double x,
      double y,
      int maxUlps)Returns true iff both arguments are equal or within the range of allowed
 error (inclusive). | 
| static long | factorial(int n)Returns n!. | 
| static double | factorialDouble(int n)Returns n!. | 
| static double | factorialLog(int n)Returns the natural logarithm of n!. | 
| static int | gcd(int p,
   int q)
 Gets the greatest common divisor of the absolute value of two numbers,
 using the "binary gcd" method which avoids division and modulo
 operations. | 
| static long | gcd(long p,
   long q)
 Gets the greatest common divisor of the absolute value of two numbers,
 using the "binary gcd" method which avoids division and modulo
 operations. | 
| static int | hash(double value)Returns an integer hash code representing the given double value. | 
| static int | hash(double[] value)Returns an integer hash code representing the given double array. | 
| static byte | indicator(byte x)For a byte value x, this method returns (byte)(+1) if x >= 0 and
 (byte)(-1) if x < 0. | 
| static double | indicator(double x)For a double precision value x, this method returns +1.0 if x >= 0 and
 -1.0 if x < 0. | 
| static float | indicator(float x)For a float value x, this method returns +1.0F if x >= 0 and -1.0F if x <
 0. | 
| static int | indicator(int x)For an int value x, this method returns +1 if x >= 0 and -1 if x < 0. | 
| static long | indicator(long x)For a long value x, this method returns +1L if x >= 0 and -1L if x < 0. | 
| static short | indicator(short x)For a short value x, this method returns (short)(+1) if x >= 0 and
 (short)(-1) if x < 0. | 
| static int | lcm(int a,
   int b)
 Returns the least common multiple of the absolute value of two numbers,
 using the formula  lcm(a,b) = (a / gcd(a,b)) * b. | 
| static long | lcm(long a,
   long b)
 Returns the least common multiple of the absolute value of two numbers,
 using the formula  lcm(a,b) = (a / gcd(a,b)) * b. | 
| static double | log(double base,
   double x) | 
| static int | mulAndCheck(int x,
           int y)Multiply two integers, checking for overflow. | 
| static long | mulAndCheck(long a,
           long b)Multiply two long integers, checking for overflow. | 
| static double | nextAfter(double d,
         double direction)Get the next machine representable number after a number, moving
 in the direction of another number. | 
| static double | normalizeAngle(double a,
              double center)Normalize an angle in a 2&pi wide interval around a center value. | 
| static double[] | normalizeArray(double[] values,
              double normalizedSum)Normalizes an array to make it sum to a specified value. | 
| static java.math.BigInteger | pow(java.math.BigInteger k,
   java.math.BigInteger e)Raise a BigInteger to a BigInteger power. | 
| static java.math.BigInteger | pow(java.math.BigInteger k,
   int e)Raise a BigInteger to an int power. | 
| static java.math.BigInteger | pow(java.math.BigInteger k,
   long e)Raise a BigInteger to a long power. | 
| static int | pow(int k,
   int e)Raise an int to an int power. | 
| static int | pow(int k,
   long e)Raise an int to a long power. | 
| static long | pow(long k,
   int e)Raise a long to an int power. | 
| static long | pow(long k,
   long e)Raise a long to a long power. | 
| static double | round(double x,
     int scale)Round the given value to the specified number of decimal places. | 
| static double | round(double x,
     int scale,
     int roundingMethod)Round the given value to the specified number of decimal places. | 
| static float | round(float x,
     int scale)Round the given value to the specified number of decimal places. | 
| static float | round(float x,
     int scale,
     int roundingMethod)Round the given value to the specified number of decimal places. | 
| static double | scalb(double d,
     int scaleFactor)Scale a number by 2scaleFactor. | 
| static byte | sign(byte x)Returns the  sign
 for byte value  x. | 
| static double | sign(double x)Returns the  sign
 for double precision  x. | 
| static float | sign(float x)Returns the  sign
 for float value  x. | 
| static int | sign(int x)Returns the  sign
 for int value  x. | 
| static long | sign(long x)Returns the  sign
 for long value  x. | 
| static short | sign(short x)Returns the  sign
 for short value  x. | 
| static double | sinh(double x)Returns the 
 hyperbolic sine of x. | 
| static int | subAndCheck(int x,
           int y)Subtract two integers, checking for overflow. | 
| static long | subAndCheck(long a,
           long b)Subtract two long integers, checking for overflow. | 
public static final double EPSILON
public static final double SAFE_MIN
In IEEE 754 arithmetic, this is also the smallest normalized number 2-1022.
public static final double TWO_PI
public static int addAndCheck(int x,
                              int y)
x - an addendy - an addendx+yjava.lang.ArithmeticException - if the result can not be represented as an
                             intpublic static long addAndCheck(long a,
                               long b)
a - an addendb - an addenda+bjava.lang.ArithmeticException - if the result can not be represented as an
                             longpublic static long binomialCoefficient(int n,
                                       int k)
n choose k", the number of
 k-element subsets that can be selected from an
 n-element set.
 Preconditions:
0 <= k <= n  (otherwise
 IllegalArgumentException is thrown)long. The
 largest value of n for which all coefficients are
  < Long.MAX_VALUE is 66. If the computed value exceeds
 Long.MAX_VALUE an ArithMeticException is
 thrown.n - the size of the setk - the size of the subsets to be countedn choose kjava.lang.IllegalArgumentException - if preconditions are not met.java.lang.ArithmeticException - if the result is too large to be represented
                                  by a long integer.public static double binomialCoefficientDouble(int n,
                                               int k)
double representation of the  Binomial
 Coefficient, "n choose k", the number of
 k-element subsets that can be selected from an
 n-element set.
 Preconditions:
0 <= k <= n  (otherwise
 IllegalArgumentException is thrown)double. The
 largest value of n for which all coefficients are <
 Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE,
 Double.POSITIVE_INFINITY is returnedn - the size of the setk - the size of the subsets to be countedn choose kjava.lang.IllegalArgumentException - if preconditions are not met.public static double binomialCoefficientLog(int n,
                                            int k)
log of the  Binomial
 Coefficient, "n choose k", the number of
 k-element subsets that can be selected from an
 n-element set.
 Preconditions:
0 <= k <= n  (otherwise
 IllegalArgumentException is thrown)n - the size of the setk - the size of the subsets to be countedn choose kjava.lang.IllegalArgumentException - if preconditions are not met.public static int compareTo(double x,
                            double y,
                            double eps)
x - the first numbery - the second numbereps - the amount of error to allow when checking for equalityequals(x, y, eps)equals(x, y, eps) && x < yequals(x, y, eps) && x > ypublic static double cosh(double x)
x - double value for which to find the hyperbolic cosinepublic static boolean equals(double x,
                             double y)
x - first valuey - second valuepublic static boolean equals(double x,
                             double y,
                             double eps)
Two NaNs are considered equals, as are two infinities with same sign.
x - first valuey - second valueeps - the amount of absolute error to allowpublic static boolean equals(double x,
                             double y,
                             int maxUlps)
x - first valuey - second valuemaxUlps - (maxUlps - 1) is the number of floating point
                values between x and y.true if there are less than maxUlps floating
 point values between x and ypublic static boolean equals(double[] x,
                             double[] y)
equalsx - first arrayy - second arraypublic static long factorial(int n)
n  Factorial, the
 product of the numbers 1,...,n.
 Preconditions:
n >= 0 (otherwise
 IllegalArgumentException is thrown)long. The
 largest value of n for which n! <
 Long.MAX_VALUE is 20. If the computed value exceeds Long.MAX_VALUE
 an ArithMeticException  is thrown.n - argumentn!java.lang.ArithmeticException - if the result is too large to be represented
                                  by a long integer.java.lang.IllegalArgumentException - if n < 0public static double factorialDouble(int n)
n  Factorial, the
 product of the numbers 1,...,n as a double.
 Preconditions:
n >= 0 (otherwise
 IllegalArgumentException is thrown)double. The
 largest value of n for which n! <
 Double.MAX_VALUE is 170. If the computed value exceeds
 Double.MAX_VALUE, Double.POSITIVE_INFINITY is returnedn - argumentn!java.lang.IllegalArgumentException - if n < 0public static double factorialLog(int n)
Preconditions:
n >= 0 (otherwise
 IllegalArgumentException is thrown)n - argumentn!java.lang.IllegalArgumentException - if preconditions are not met.public static int gcd(int p,
                      int q)
Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef Stein (1961). Special cases:
gcd(Integer.MIN_VALUE, Integer.MIN_VALUE),
 gcd(Integer.MIN_VALUE, 0) and
 gcd(0, Integer.MIN_VALUE) throw an
 ArithmeticException, because the result would be 2^31, which
 is too large for an int value.gcd(x, x), gcd(0, x) and
 gcd(x, 0) is the absolute value of x, except
 for the special cases above.
 gcd(0, 0) is the only one which returns
 0.p - any numberq - any numberjava.lang.ArithmeticException - if the result cannot be represented as a
                             nonnegative int valuepublic static long gcd(long p,
                       long q)
Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef Stein (1961). Special cases:
gcd(Long.MIN_VALUE, Long.MIN_VALUE),
 gcd(Long.MIN_VALUE, 0L) and
 gcd(0L, Long.MIN_VALUE) throw an
 ArithmeticException, because the result would be 2^63, which
 is too large for a long value.gcd(x, x), gcd(0L, x) and
 gcd(x, 0L) is the absolute value of x, except
 for the special cases above.
 gcd(0L, 0L) is the only one which returns
 0L.p - any numberq - any numberjava.lang.ArithmeticException - if the result cannot be represented as a nonnegative long
                             valuepublic static int hash(double value)
value - the value to be hashedpublic static int hash(double[] value)
value - the value to be hashed (may be null)public static byte indicator(byte x)
x - the value, a bytepublic static double indicator(double x)
NaN if x is
 NaN.x - the value, a doublepublic static float indicator(float x)
NaN if x is NaN.x - the value, a floatpublic static int indicator(int x)
x - the value, an intpublic static long indicator(long x)
x - the value, a longpublic static short indicator(short x)
x - the value, a shortpublic static int lcm(int a,
                      int b)
 Returns the least common multiple of the absolute value of two numbers,
 using the formula lcm(a,b) = (a / gcd(a,b)) * b.
 Special cases:
 
lcm(Integer.MIN_VALUE, n) and
 lcm(n, Integer.MIN_VALUE), where abs(n) is a
 power of 2, throw an ArithmeticException, because the result
 would be 2^31, which is too large for an int value.lcm(0, x) and lcm(x, 0) is
 0 for any x.
 a - any numberb - any numberjava.lang.ArithmeticException - if the result cannot be represented as a nonnegative int
                             valuepublic static long lcm(long a,
                       long b)
 Returns the least common multiple of the absolute value of two numbers,
 using the formula lcm(a,b) = (a / gcd(a,b)) * b.
 Special cases:
 
lcm(Long.MIN_VALUE, n) and
 lcm(n, Long.MIN_VALUE), where abs(n) is a
 power of 2, throw an ArithmeticException, because the result
 would be 2^63, which is too large for an int value.lcm(0L, x) and lcm(x, 0L) is
 0L for any x.
 a - any numberb - any numberjava.lang.ArithmeticException - if the result cannot be represented as a nonnegative long
                             valuepublic static double log(double base,
                         double x)
Returns the
 logarithm
 for base b of x.
 
Returns NaN if either argument is negative.  If
 base is 0 and x is positive, 0 is returned.
 If base is positive and x is 0,
 Double.NEGATIVE_INFINITY is returned.  If both arguments
 are 0, the result is NaN.
base - the base of the logarithm, must be greater than 0x - argument, must be greater than 0public static int mulAndCheck(int x,
                              int y)
x - a factory - a factorx*yjava.lang.ArithmeticException - if the result can not be represented as an
                             intpublic static long mulAndCheck(long a,
                               long b)
a - first valueb - second valuea * bjava.lang.ArithmeticException - if the result can not be represented as an
                             longpublic static double nextAfter(double d,
                               double direction)
 If direction is greater than or equal tod,
 the smallest machine representable number strictly greater than
 d is returned; otherwise the largest representable number
 strictly less than d is returned.
 If d is NaN or Infinite, it is returned unchanged.
d - base numberdirection - (the only important thing is whether
                  direction is greater or smaller than d)public static double scalb(double d,
                           int scaleFactor)
If d is 0 or NaN or Infinite, it is returned unchanged.
d - base numberscaleFactor - power of two by which d sould be multipliedpublic static double normalizeAngle(double a,
                                    double center)
This method has three main uses:
a = MathUtils.normalizeAngle(a, Math.PI);a = MathUtils.normalizeAngle(a, 0.0);angle = MathUtils.normalizeAngle(end, start) - start;Note that due to numerical accuracy and since π cannot be represented exactly, the result interval is closed, it cannot be half-closed as would be more satisfactory in a purely mathematical view.
a - angle to normalizecenter - center of the desired 2π interval for the resultpublic static double[] normalizeArray(double[] values,
                                      double normalizedSum)
                               throws java.lang.ArithmeticException,
                                      java.lang.IllegalArgumentException
Normalizes an array to make it sum to a specified value. Returns the result of the transformation
    x |-> x * normalizedSum / sum
 
 applied to each non-NaN element x of the input array, where sum is the
 sum of the non-NaN entries in the input array.
 Throws IllegalArgumentException if normalizedSum is infinite
 or NaN and ArithmeticException if the input array contains any infinite elements
 or sums to 0
Ignores (i.e., copies unchanged to the output array) NaNs in the input array.
values - input array to be normalizednormalizedSum - target sum for the normalized arrayjava.lang.ArithmeticException - if the input array contains infinite elements or sums to zerojava.lang.IllegalArgumentException - if the target sum is infinite or NaNpublic static double round(double x,
                           int scale)
BigDecimal.ROUND_HALF_UP method.x - the value to round.scale - the number of digits to the right of the decimal point.public static double round(double x,
                           int scale,
                           int roundingMethod)
BigDecimal.x - the value to round.scale - the number of digits to the right of the decimal point.roundingMethod - the rounding method as defined in
                       BigDecimal.public static float round(float x,
                          int scale)
BigDecimal.ROUND_HALF_UP method.x - the value to round.scale - the number of digits to the right of the decimal point.public static float round(float x,
                          int scale,
                          int roundingMethod)
BigDecimal.x - the value to round.scale - the number of digits to the right of the decimal point.roundingMethod - the rounding method as defined in
                       BigDecimal.public static byte sign(byte x)
x.
 For a byte value x, this method returns (byte)(+1) if x > 0, (byte)(0) if x = 0, and (byte)(-1) if x < 0.
x - the value, a bytepublic static double sign(double x)
x.
 
 For a double value x, this method returns
 +1.0 if x > 0, 0.0 if
 x = 0.0, and -1.0 if x < 0.
 Returns NaN if x is NaN.
x - the value, a doublepublic static float sign(float x)
x.
 
 For a float value x, this method returns +1.0F if x > 0, 0.0F if x =
 0.0F, and -1.0F if x < 0. Returns NaN if x
 is NaN.
x - the value, a floatpublic static int sign(int x)
x.
 For an int value x, this method returns +1 if x > 0, 0 if x = 0, and -1 if x < 0.
x - the value, an intpublic static long sign(long x)
x.
 For a long value x, this method returns +1L if x > 0, 0L if x = 0, and -1L if x < 0.
x - the value, a longpublic static short sign(short x)
x.
 For a short value x, this method returns (short)(+1) if x > 0, (short)(0) if x = 0, and (short)(-1) if x < 0.
x - the value, a shortpublic static double sinh(double x)
x - double value for which to find the hyperbolic sinepublic static int subAndCheck(int x,
                              int y)
x - the minuendy - the subtrahendx-yjava.lang.ArithmeticException - if the result can not be represented as an
                             intpublic static long subAndCheck(long a,
                               long b)
a - first valueb - second valuea-bjava.lang.ArithmeticException - if the result can not be represented as an
                             longpublic static int pow(int k,
                      int e)
               throws java.lang.IllegalArgumentException
k - number to raisee - exponent (must be positive or null)java.lang.IllegalArgumentException - if e is negativepublic static int pow(int k,
                      long e)
               throws java.lang.IllegalArgumentException
k - number to raisee - exponent (must be positive or null)java.lang.IllegalArgumentException - if e is negativepublic static long pow(long k,
                       int e)
                throws java.lang.IllegalArgumentException
k - number to raisee - exponent (must be positive or null)java.lang.IllegalArgumentException - if e is negativepublic static long pow(long k,
                       long e)
                throws java.lang.IllegalArgumentException
k - number to raisee - exponent (must be positive or null)java.lang.IllegalArgumentException - if e is negativepublic static java.math.BigInteger pow(java.math.BigInteger k,
                                       int e)
                                throws java.lang.IllegalArgumentException
k - number to raisee - exponent (must be positive or null)java.lang.IllegalArgumentException - if e is negativepublic static java.math.BigInteger pow(java.math.BigInteger k,
                                       long e)
                                throws java.lang.IllegalArgumentException
k - number to raisee - exponent (must be positive or null)java.lang.IllegalArgumentException - if e is negativepublic static java.math.BigInteger pow(java.math.BigInteger k,
                                       java.math.BigInteger e)
                                throws java.lang.IllegalArgumentException
k - number to raisee - exponent (must be positive or null)java.lang.IllegalArgumentException - if e is negativepublic static double distance1(double[] p1,
                               double[] p2)
p1 - the first pointp2 - the second pointpublic static int distance1(int[] p1,
                            int[] p2)
p1 - the first pointp2 - the second pointpublic static double distance(double[] p1,
                              double[] p2)
p1 - the first pointp2 - the second pointpublic static double distance(int[] p1,
                              int[] p2)
p1 - the first pointp2 - the second pointpublic static double distanceInf(double[] p1,
                                 double[] p2)
p1 - the first pointp2 - the second pointpublic static int distanceInf(int[] p1,
                              int[] p2)
p1 - the first pointp2 - the second pointpublic static void checkOrder(double[] val,
                              int dir,
                              boolean strict)
val - Valuesdir - Order direction (-1 for decreasing, 1 for increasing)strict - Whether the order should be strictjava.lang.IllegalArgumentException - if the array is not sorted.