Class MathUtils


  • public final class MathUtils
    extends Object
    Collection of simple mathematical routines.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int bitMix​(int in)
      Bit-mixing for pseudo-randomization of integers (e.g., to guard against bad hash functions).
      static int checkedDownCast​(long value)
      Casts the given value to a 32 bit integer, if it can be safely done.
      static int divideRoundUp​(int dividend, int divisor)
      Divide and rounding up to integer.
      static long flipSignBit​(long in)
      Flips the sign bit (most-significant-bit) of the input.
      static boolean isPowerOf2​(long value)
      Checks whether the given value is a power of two.
      static int jenkinsHash​(int code)
      This function hashes an integer value.
      static int log2floor​(int value)
      Computes the logarithm of the given value to the base of 2, rounded down.
      static int log2strict​(int value)
      Computes the logarithm of the given value to the base of 2.
      static int longToIntWithBitMixing​(long in)
      Pseudo-randomly maps a long (64-bit) to an integer (32-bit) using some bit-mixing for better distribution.
      static int murmurHash​(int code)
      This function hashes an integer value.
      static int roundDownToPowerOf2​(int value)
      Decrements the given number down to the closest power of two.
      static int roundUpToPowerOfTwo​(int x)
      Round the given number to the next power of two.
    • Method Detail

      • log2floor

        public static int log2floor​(int value)
                             throws ArithmeticException
        Computes the logarithm of the given value to the base of 2, rounded down. It corresponds to the position of the highest non-zero bit. The position is counted, starting with 0 from the least significant bit to the most significant bit. For example, log2floor(16) = 4 , and log2floor(10) = 3.
        Parameters:
        value - The value to compute the logarithm for.
        Returns:
        The logarithm (rounded down) to the base of 2.
        Throws:
        ArithmeticException - Thrown, if the given value is zero.
      • log2strict

        public static int log2strict​(int value)
                              throws ArithmeticException,
                                     IllegalArgumentException
        Computes the logarithm of the given value to the base of 2. This method throws an error, if the given argument is not a power of 2.
        Parameters:
        value - The value to compute the logarithm for.
        Returns:
        The logarithm to the base of 2.
        Throws:
        ArithmeticException - Thrown, if the given value is zero.
        IllegalArgumentException - Thrown, if the given value is not a power of two.
      • roundDownToPowerOf2

        public static int roundDownToPowerOf2​(int value)
        Decrements the given number down to the closest power of two. If the argument is a power of two, it remains unchanged.
        Parameters:
        value - The value to round down.
        Returns:
        The closest value that is a power of two and less or equal than the given value.
      • checkedDownCast

        public static int checkedDownCast​(long value)
        Casts the given value to a 32 bit integer, if it can be safely done. If the cast would change the numeric value, this method raises an exception.

        This method is a protection in places where one expects to be able to safely case, but where unexpected situations could make the cast unsafe and would cause hidden problems that are hard to track down.

        Parameters:
        value - The value to be cast to an integer.
        Returns:
        The given value as an integer.
        See Also:
        Math.toIntExact(long)
      • isPowerOf2

        public static boolean isPowerOf2​(long value)
        Checks whether the given value is a power of two.
        Parameters:
        value - The value to check.
        Returns:
        True, if the value is a power of two, false otherwise.
      • jenkinsHash

        public static int jenkinsHash​(int code)
        This function hashes an integer value. It is adapted from Bob Jenkins' website http://www.burtleburtle.net/bob/hash/integer.html. The hash function has the full avalanche property, meaning that every bit of the value to be hashed affects every bit of the hash value.

        It is crucial to use different hash functions to partition data across machines and the internal partitioning of data structures. This hash function is intended for partitioning internally in data structures.

        Parameters:
        code - The integer to be hashed.
        Returns:
        The non-negative hash code for the integer.
      • murmurHash

        public static int murmurHash​(int code)
        This function hashes an integer value.

        It is crucial to use different hash functions to partition data across machines and the internal partitioning of data structures. This hash function is intended for partitioning across machines.

        Parameters:
        code - The integer to be hashed.
        Returns:
        The non-negative hash code for the integer.
      • roundUpToPowerOfTwo

        public static int roundUpToPowerOfTwo​(int x)
        Round the given number to the next power of two.
        Parameters:
        x - number to round
        Returns:
        x rounded up to the next power of two
      • longToIntWithBitMixing

        public static int longToIntWithBitMixing​(long in)
        Pseudo-randomly maps a long (64-bit) to an integer (32-bit) using some bit-mixing for better distribution.
        Parameters:
        in - the long (64-bit)input.
        Returns:
        the bit-mixed int (32-bit) output
      • bitMix

        public static int bitMix​(int in)
        Bit-mixing for pseudo-randomization of integers (e.g., to guard against bad hash functions). Implementation is from Murmur's 32 bit finalizer.
        Parameters:
        in - the input value
        Returns:
        the bit-mixed output value
      • flipSignBit

        public static long flipSignBit​(long in)
        Flips the sign bit (most-significant-bit) of the input.
        Parameters:
        in - the input value.
        Returns:
        the input with a flipped sign bit (most-significant-bit).
      • divideRoundUp

        public static int divideRoundUp​(int dividend,
                                        int divisor)
        Divide and rounding up to integer. E.g., divideRoundUp(3, 2) returns 2, divideRoundUp(0, 3) returns 0. Note that this method does not support negative values.
        Parameters:
        dividend - value to be divided by the divisor
        divisor - value by which the dividend is to be divided
        Returns:
        the quotient rounding up to integer