Class XMath


  • public final class XMath
    extends Object
    • Constructor Summary

      Constructors 
      Constructor Description
      XMath()  
    • Method Summary

      Modifier and Type Method Description
      static long addCapped​(long l1, long l2)  
      static double avg​(double... values)  
      static float avg​(float... values)  
      static int avg​(int... values)  
      static long avg​(long... values)  
      static BigDecimal bigDec​(double value)
      Alias for BigDecimal.valueOf(value)
      static BigDecimal bigDec​(long value)
      Alias for BigDecimal.valueOf(value)
      static BigInteger bigInt​(int value)
      Alias for BigInteger.valueOf(value)
      static BigInteger bigInt​(long value)
      Alias for BigInteger.valueOf(value)
      static int cap_int​(long value)  
      static double[] column​(int columnIndex, double[]... matrix)  
      static double columnSum​(int columnIndex, double[]... matrix)  
      static double columnSum​(int columnIndex, Double[]... matrix)  
      static double cube​(double d)  
      static float cube​(float f)  
      static int cube​(int i)  
      static long cube​(long l)  
      static long equal​(long value1, long value2)  
      static int even​(int value)  
      static long even​(long value)  
      static int factorial​(int n)
      Use factorial(long) for n in [0;20].
      Use factorial(BigInteger) for any n greater than 0.
      static long factorial​(long n)
      Use factorial(BigInteger) for any n greater than 0.
      static BigInteger factorial​(BigInteger n)  
      static double fractionToPercent​(double decimalFractionValue)  
      static int highestPowerOf2_int()  
      static boolean isGreaterThanHighestPowerOf2​(int value)  
      static boolean isGreaterThanHighestPowerOf2​(long value)  
      static boolean isGreaterThanOrEqualHighestPowerOf2​(int value)  
      static boolean isGreaterThanOrEqualHighestPowerOf2​(long value)  
      static boolean isIn​(int value, int... searchValues)  
      static boolean isPow2​(int value)
      Determines if the passed value is a power-of-2 value.
      static int log10discrete​(int value)
      Returns the integer value of base-10 logarithm of the passed value.
      static int log2pow2​(int pow2Value)  
      static double lowerRatio​(double a, double b)
      Returns abs(d1/d2) for abs(d1) lower than abs(d2), else abs(d2/d1) in order to guarantee a codomain of [0.0;1.0]
      static double max​(double... values)  
      static float max​(float... values)  
      static int max​(int... values)  
      static long max​(long... values)  
      static double min​(double... values)  
      static float min​(float... values)  
      static int min​(int... values)  
      static long min​(long... values)  
      static <E> double min_double​(Iterable<E> elements, To_double<? super E> getter)  
      static <E> double min_double​(Iterable<E> elements, To_double<? super E> getter, double defaultValue)  
      static <E> Double minDouble​(Iterable<E> elements, To_double<? super E> getter)  
      static int negative​(int value)  
      static long negative​(long value)  
      static double notNegative​(double value)  
      static int notNegative​(int value)  
      static long notNegative​(long value)  
      static Integer notNegative​(Integer value)  
      static Long notNegative​(Long value)  
      static double notNegativeMax1​(double value)  
      static int odd​(int value)  
      static long odd​(long value)  
      static Double one()  
      static double percentToFraction​(double decimalPercentValue)  
      static double positive​(double value)  
      static float positive​(float value)  
      static int positive​(int value)  
      static long positive​(long value)  
      static double positiveMax1​(double value)  
      static double pow​(double base, int exponent)  
      static float pow​(float base, int exponent)  
      static int pow​(int base, int exponent)
      This method is an int version of Math.pow(double, double), using only integer iteration for calculation.
      static int pow10​(int exponent)  
      static double pow10_double​(int exponent)  
      static long pow10_long​(int exponent)  
      static int pow2Bound​(int n)  
      static int pow2BoundCapped​(int n)  
      static int pow2BoundMaxed​(int n)  
      static Random random()  
      static int random​(int n)  
      static int[] randoming​(int length)  
      static int[] randoming​(int length, int bound)  
      static int[] randoming​(int length, int lowestValue, int bound)  
      static _longRange range​(int start, int bound)  
      static double round​(double value, int decimals)
      Rounds value to the actual closest value for decimals decimals.
      This is useful as well in order to "normalize" values if multiple subsequent calulations with double values accumulate rounding errors that drift the value away from the value it actually should (could) be.
      See the "candy" example in Joshua Bloch's "Effective Java": this method fixes the problem.
      static double round0​(double value)  
      static double round1​(double value)
      Common rounding variant for 1 decimal.
      static double round2​(double value)
      Common rounding variant for 2 decimals.
      static double round3​(double value)
      Common rounding variant for 3 decimals.
      static double round4​(double value)
      Common rounding variant for 4 decimals.
      static double round5​(double value)
      Common rounding variant for 5 decimals.
      static double round6​(double value)
      Common rounding variant for 6 decimals.
      static double round7​(double value)
      Common rounding variant for 7 decimals.
      static double round8​(double value)
      Common rounding variant for 8 decimals.
      static double round9​(double value)
      Common rounding variant for 9 decimals.
      static byte[] sequence​(byte from, byte to)  
      static int[] sequence​(int lastValue)  
      static int[] sequence​(int from, int to)  
      static long[] sequence​(long from, long to)  
      static short[] sequence​(short from, short to)  
      static Integer[] sequence​(Integer lastValue)  
      static double square​(double d)  
      static float square​(float f)  
      static int square​(int i)  
      static long square​(long l)  
      static int stringLength​(int value)
      Returns the amount of digits the passed values requires to be projected as a string.
      static long sum​(byte... values)  
      static double sum​(double... values)  
      static double sum​(float... values)  
      static long sum​(int... values)  
      static long sum​(long... values)  
      static long sum​(short... values)  
      static Double zero()  
    • Constructor Detail

      • XMath

        public XMath()
    • Method Detail

      • zero

        public static Double zero()
      • one

        public static Double one()
      • pow

        public static final int pow​(int base,
                                    int exponent)
                             throws IllegalArgumentException
        This method is an int version of Math.pow(double, double), using only integer iteration for calculation.

        As a rule of thumb:
        It is faster for exponent lower than 250 (significantly faster for exponents lt 100) and slower for exponent greater than or equal 250 (significantly slower for exponents gt/e 500).
        This may depend on the concrete system running the program, of course.
        Note that exponent may not be negative, otherwise an IllegalArgumentException is thrown.

        Parameters:
        base -
        exponent - my not be negative
        Returns:
        base^exponent
        Throws:
        IllegalArgumentException - if exponent is negative
      • pow2BoundMaxed

        public static final int pow2BoundMaxed​(int n)
      • pow2BoundCapped

        public static final int pow2BoundCapped​(int n)
      • pow2Bound

        public static final int pow2Bound​(int n)
      • log2pow2

        public static final int log2pow2​(int pow2Value)
      • isPow2

        public static final boolean isPow2​(int value)
        Determines if the passed value is a power-of-2 value.
        Parameters:
        value -
        Returns:
        true for any n in [1;30] that fulfills value = 2^n
      • log10discrete

        public static final int log10discrete​(int value)
                                       throws IllegalArgumentException
        Returns the integer value of base-10 logarithm of the passed value.

        Examples:

         log10(1) = 0
         log10(8) = 0
         log10(10) = 1
         log10(99) = 1
         log10(100) = 2
         log10(1000000000) = 9
         log10(2147483647) = 9
         
        Note that passing a value lower than or equal 0 will throw an IllegalArgumentException.
        Parameters:
        value -
        Throws:
        IllegalArgumentException
      • stringLength

        public static final int stringLength​(int value)
        Returns the amount of digits the passed values requires to be projected as a string.

        The additional length for the minus of to represent negative values is acounted for as well.
        Examples:

         stringLength(0) = 1
         stringLength(+6) = 1
         stringLength(10) = 2
         stringLength(+2147483647) = 10
         stringLength(-1) = 2
         stringLength(-2147483648) = 11
         
        Parameters:
        value - the int value whose string length shall be calculated
        Returns:
        the length the passed value's string representation will require.
      • square

        public static final float square​(float f)
      • square

        public static final long square​(long l)
      • square

        public static final int square​(int i)
      • square

        public static final double square​(double d)
      • cube

        public static final float cube​(float f)
      • cube

        public static final long cube​(long l)
      • cube

        public static final int cube​(int i)
      • cube

        public static final double cube​(double d)
      • round0

        public static final double round0​(double value)
      • round1

        public static final double round1​(double value)
        Common rounding variant for 1 decimal.
        Parameters:
        value - the decimal value to be rounded.
        Returns:
        the passed value rounded to 1 decimal.
      • round2

        public static final double round2​(double value)
        Common rounding variant for 2 decimals.
        Parameters:
        value - the decimal value to be rounded.
        Returns:
        the passed value rounded to 2 decimals.
      • round3

        public static final double round3​(double value)
        Common rounding variant for 3 decimals.
        Parameters:
        value - the decimal value to be rounded.
        Returns:
        the passed value rounded to 3 decimals.
      • round4

        public static final double round4​(double value)
        Common rounding variant for 4 decimals.
        Parameters:
        value - the decimal value to be rounded.
        Returns:
        the passed value rounded to 4 decimals.
      • round5

        public static final double round5​(double value)
        Common rounding variant for 5 decimals.
        Parameters:
        value - the decimal value to be rounded.
        Returns:
        the passed value rounded to 5 decimals.
      • round6

        public static final double round6​(double value)
        Common rounding variant for 6 decimals.
        Parameters:
        value - the decimal value to be rounded.
        Returns:
        the passed value rounded to 6 decimals.
      • round7

        public static final double round7​(double value)
        Common rounding variant for 7 decimals.
        Parameters:
        value - the decimal value to be rounded.
        Returns:
        the passed value rounded to 7 decimals.
      • round8

        public static final double round8​(double value)
        Common rounding variant for 8 decimals.
        Parameters:
        value - the decimal value to be rounded.
        Returns:
        the passed value rounded to 8 decimals.
      • round9

        public static final double round9​(double value)
        Common rounding variant for 9 decimals.
        Parameters:
        value - the decimal value to be rounded.
        Returns:
        the passed value rounded to 9 decimals.
      • round

        public static final double round​(double value,
                                         int decimals)
        Rounds value to the actual closest value for decimals decimals.
        This is useful as well in order to "normalize" values if multiple subsequent calulations with double values accumulate rounding errors that drift the value away from the value it actually should (could) be.
        See the "candy" example in Joshua Bloch's "Effective Java": this method fixes the problem.

        Note that decimals may not be negative.
        Also note that while a value of 0 for decimals will yield the correct result, it makes not much sense to call this method for it in the first place.

        Parameters:
        value - any double value
        decimals - the number of decimals. May not be negative.
        Returns:
        the normalized value for value
      • range

        public static _longRange range​(int start,
                                       int bound)
      • sequence

        public static byte[] sequence​(byte from,
                                      byte to)
      • sequence

        public static short[] sequence​(short from,
                                       short to)
      • sequence

        public static int[] sequence​(int lastValue)
      • randoming

        public static int[] randoming​(int length)
      • randoming

        public static int[] randoming​(int length,
                                      int bound)
      • randoming

        public static int[] randoming​(int length,
                                      int lowestValue,
                                      int bound)
      • sequence

        public static int[] sequence​(int from,
                                     int to)
      • max

        public static final double max​(double... values)
      • max

        public static final float max​(float... values)
      • max

        public static final int max​(int... values)
      • max

        public static final long max​(long... values)
      • min

        public static final double min​(double... values)
      • min

        public static final float min​(float... values)
      • min

        public static final int min​(int... values)
      • min

        public static final long min​(long... values)
      • sum

        public static final long sum​(byte... values)
      • sum

        public static final long sum​(short... values)
      • sum

        public static final long sum​(int... values)
      • sum

        public static final double sum​(float... values)
      • sum

        public static final long sum​(long... values)
      • sum

        public static final double sum​(double... values)
      • columnSum

        public static final double columnSum​(int columnIndex,
                                             double[]... matrix)
      • columnSum

        public static final double columnSum​(int columnIndex,
                                             Double[]... matrix)
      • avg

        public static final double avg​(double... values)
      • avg

        public static final float avg​(float... values)
      • avg

        public static final int avg​(int... values)
      • avg

        public static final long avg​(long... values)
      • lowerRatio

        public static final double lowerRatio​(double a,
                                              double b)
        Returns abs(d1/d2) for abs(d1) lower than abs(d2), else abs(d2/d1) in order to guarantee a codomain of [0.0;1.0]
        Parameters:
        a - the first value
        b - the second value
        Returns:
        the lower ratio of d1 and d2
      • factorial

        public static final int factorial​(int n)
                                   throws IllegalArgumentException
        Use factorial(long) for n in [0;20].
        Use factorial(BigInteger) for any n greater than 0.
        Parameters:
        n - natural number in [0;12]
        Returns:
        n!
        Throws:
        IllegalArgumentException - for n lower than 0 or n greater than 12.
      • factorial

        public static final long factorial​(long n)
                                    throws IllegalArgumentException
        Use factorial(BigInteger) for any n greater than 0.
        Parameters:
        n - natural number in [0;20]
        Returns:
        n!
        Throws:
        IllegalArgumentException - for n lower than 0 or n greater than 20
      • bigInt

        public static final BigInteger bigInt​(int value)
        Alias for BigInteger.valueOf(value)
        Parameters:
        value - any value
        Returns:
        a BigInteger representing value
      • bigInt

        public static final BigInteger bigInt​(long value)
        Alias for BigInteger.valueOf(value)
        Parameters:
        value - any value
        Returns:
        a BigInteger representing value
      • bigDec

        public static final BigDecimal bigDec​(long value)
        Alias for BigDecimal.valueOf(value)
        Parameters:
        value - any value
        Returns:
        a BigDecimal representing value
      • bigDec

        public static final BigDecimal bigDec​(double value)
        Alias for BigDecimal.valueOf(value)
        Parameters:
        value - any value
        Returns:
        a BigDecimal representing value
      • random

        public static final Random random()
        Returns:
        the random
      • random

        public static final int random​(int n)
      • even

        public static int even​(int value)
      • even

        public static long even​(long value)
      • odd

        public static int odd​(int value)
      • odd

        public static long odd​(long value)
      • column

        public static double[] column​(int columnIndex,
                                      double[]... matrix)
      • cap_int

        public static final int cap_int​(long value)
      • isGreaterThanOrEqualHighestPowerOf2

        public static final boolean isGreaterThanOrEqualHighestPowerOf2​(long value)
      • isGreaterThanHighestPowerOf2

        public static final boolean isGreaterThanHighestPowerOf2​(long value)
      • isGreaterThanOrEqualHighestPowerOf2

        public static final boolean isGreaterThanOrEqualHighestPowerOf2​(int value)
      • isGreaterThanHighestPowerOf2

        public static final boolean isGreaterThanHighestPowerOf2​(int value)
      • highestPowerOf2_int

        public static final int highestPowerOf2_int()
      • fractionToPercent

        public static final double fractionToPercent​(double decimalFractionValue)
      • percentToFraction

        public static final double percentToFraction​(double decimalPercentValue)
      • isIn

        public static final boolean isIn​(int value,
                                         int... searchValues)
      • addCapped

        public static long addCapped​(long l1,
                                     long l2)
      • min_double

        public static <E> double min_double​(Iterable<E> elements,
                                            To_double<? super E> getter)
      • min_double

        public static <E> double min_double​(Iterable<E> elements,
                                            To_double<? super E> getter,
                                            double defaultValue)