Class MathUtil

java.lang.Object
io.deephaven.base.MathUtil

public class MathUtil extends Object
A handful of simple mathematical utilities.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The maximum power of 2.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    base10digits(int n)
    Compute the number of base 10 digits in n's representation, for n >= 0.
    static int
    ceilLog2(int x)
    Compute ceil(log2(x)).
    static int
    ceilLog2(long x)
    Compute ceil(log2(x)).
    static int
    floorLog2(int x)
    Compute floor(log2(x)).
    static int
    floorLog2(long x)
    Compute floor(log2(x)).
    static int
    gcd(int a, int b)
    Compute the greatest common divisor of two integers using the Euclidean algorithm.
    static int
    pow10(int n)
    Compute 10^n as a int for 0 <= n <= 9.
    static int
    roundUpArraySize(int size)
    Rounds up to the next power of 2 for size <= MAX_POWER_OF_2, otherwise returns ArrayUtil.MAX_ARRAY_SIZE.
    static int
    Rounds up to the next power of 2 for x; if x is already a power of 2, x will be returned.
    static long
    Rounds up to the next power of 2 for x; if x is already a power of 2, x will be returned.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_POWER_OF_2

      public static final int MAX_POWER_OF_2
      The maximum power of 2.
      See Also:
  • Constructor Details

    • MathUtil

      public MathUtil()
  • Method Details

    • ceilLog2

      public static int ceilLog2(int x)
      Compute ceil(log2(x)). See Integer.numberOfLeadingZeros(int).
      Parameters:
      x - Input
      Returns:
      ceil(log2(x))
    • floorLog2

      public static int floorLog2(int x)
      Compute floor(log2(x)). See Integer.numberOfLeadingZeros(int).
      Parameters:
      x - Input
      Returns:
      floor(log2(x))
    • ceilLog2

      public static int ceilLog2(long x)
      Compute ceil(log2(x)). See Long.numberOfLeadingZeros(long).
      Parameters:
      x - Input
      Returns:
      ceil(log2(x))
    • floorLog2

      public static int floorLog2(long x)
      Compute floor(log2(x)). See Long.numberOfLeadingZeros(long).
      Parameters:
      x - Input
      Returns:
      floor(log2(x))
    • gcd

      public static int gcd(int a, int b)
      Compute the greatest common divisor of two integers using the Euclidean algorithm.
      Parameters:
      a - The first input
      b - The second input
      Returns:
      The GCD
      ImplNote:
      Always gives a non-negative result.
    • pow10

      public static int pow10(int n)
      Compute 10^n as a int for 0 <= n <= 9.
      Parameters:
      n - the exponent
      Returns:
      10^n
    • base10digits

      public static int base10digits(int n)
      Compute the number of base 10 digits in n's representation, for n >= 0.
      Parameters:
      n - an integer >= 0
      Returns:
      how many digits in n's base 10 representation.
    • roundUpPowerOf2

      public static int roundUpPowerOf2(int x)
      Rounds up to the next power of 2 for x; if x is already a power of 2, x will be returned. Values outside the range 1 <= x <= MAX_POWER_OF_2 will return 1.

      Equivalent to Math.max(Integer.highestOneBit(x - 1) << 1, 1).

      Parameters:
      x - the value
      Returns:
      the next power of 2 for x
      See Also:
    • roundUpPowerOf2

      public static long roundUpPowerOf2(long x)
      Rounds up to the next power of 2 for x; if x is already a power of 2, x will be returned. Values outside the range 1 <= x <= Long.MAX_VALUE will return 1.

      Equivalent to Math.max(Long.highestOneBit(x - 1) << 1, 1).

      Parameters:
      x - the value
      Returns:
      the next power of 2 for x
    • roundUpArraySize

      public static int roundUpArraySize(int size)
      Rounds up to the next power of 2 for size <= MAX_POWER_OF_2, otherwise returns ArrayUtil.MAX_ARRAY_SIZE.

      Equivalent to size <= MAX_POWER_OF_2 ? roundUpPowerOf2(size) : ArrayUtil.MAX_ARRAY_SIZE.

      Parameters:
      size - the size
      Returns:
      the
      See Also: