Class MathUtil

java.lang.Object
com.pnfsoftware.jeb.util.math.MathUtil

public class MathUtil extends Object
Set of common operations on Number that are not in Math.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    almostEquals(double a, double b)
    Check for almost equality: tolerate a 1e-7 gap to make up for numerical errors inherent to IEEE754 calculations.
    static boolean
    almostEquals(double a, double b, double epsilon)
    Check for almost equality: tolerate an epsilon gap to make up for numerical errors inherent to IEEE754 calculations.
    static double
    avg(byte[] array)
    Compute the arithmetic mean of a byte array.
    static double
    avg(char[] array)
    Compute the arithmetic mean of a char array.
    static double
    avg(double[] array)
    Compute the arithmetic mean of a double array.
    static double
    avg(float[] array)
    Compute the arithmetic mean of a float array.
    static double
    avg(int[] array)
    Compute the arithmetic mean of an int array.
    static double
    avg(long[] array)
    Compute the arithmetic mean of a long array.
    static double
    avg(short[] array)
    Compute the arithmetic mean of a short array.
    static double
    avg(Collection<? extends Number> collection)
    Compute the arithmetic mean of a collection of numbers.
    static boolean
    betweenExclusive(long value, long min, long max)
    Check that a value is between two bounds (exclusive).
    static boolean
    betweenInclusive(long value, long min, long max)
    Check that a value is between two bounds (inclusive).
    static int
    bitcount(int value)
    Count the number of set bits in an integer.
    static int
    bitcount(long value)
    Count the number of set bits in a long integer.
    static boolean
    isPowerOfTwo(int n)
    Determine whether a strictly positive integer is a power of 2.
    static boolean
    isPowerOfTwo(long n)
    Determine whether a strictly positive integer is a power of 2.
    static int
    log2(int n)
    Get the truncated base2 log of the input.
    static int
    log2(long n)
    Get the truncated base2 log of the input.
    static long
    makeInverseMask(int bitsize)
    Create a 64-bit inverse mask.
    static long
    makeMask(int bitsize)
    Create a 64-bit mask.
    static long
    makeOverflow(int bitsize)
    Create a 64-bit overflow.
    static int
    moduloPositive(int a, int b)
    Calculate the positive remainder of two integers.
    static int
    msb(long value, int bitsize)
    Retrieve the most significant bit of a fixed-width value.
    static int
    pow(int a, int b)
    Integer exponentiation.
    static long
    pow(long a, long b)
    Long int exponentiation.
    static long
    signExtend(long value, int bitsize)
    Sign-extend a value to 64 bits.
    static int
    signExtend32(int value, int bitsize)
    Sign-extend a value to 32 bits.
    static long
    signExtend32(long value, int bitsize)
    Sign-extend a value to 32 bits.
    static long
    unmaskedArithShiftRight(long v, int cnt)
    Arithmetically right-shift a long value without Java's 6-bit shift-count masking.
    static long
    unmaskedShiftLeft(long v, int cnt)
    Left-shift a long value without Java's 6-bit shift-count masking.
    static long
    unmaskedShiftRight(long v, int cnt)
    Logically right-shift a long value without Java's 6-bit shift-count masking.
    static long
    zeroExtend(long value, int bitsize)
    Zero-extend a value to 64 bits.
    static long
    zeroExtend32(long value, int bitsize)
    Zero-extend a value to 32 bits.

    Methods inherited from class java.lang.Object

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

    • almostEquals

      public static boolean almostEquals(double a, double b)
      Check for almost equality: tolerate a 1e-7 gap to make up for numerical errors inherent to IEEE754 calculations.
      Parameters:
      a - first value
      b - second value
      Returns:
      true if |a-b| <= 1e-7
    • almostEquals

      public static boolean almostEquals(double a, double b, double epsilon)
      Check for almost equality: tolerate an epsilon gap to make up for numerical errors inherent to IEEE754 calculations.
      Parameters:
      a - first value
      b - second value
      epsilon - a positive number
      Returns:
      true if |a-b| <= epsilon
    • makeMask

      public static long makeMask(int bitsize)
      Create a 64-bit mask. Safe replacement for (ONE << BITSIZE) - ONE constructs.
      Parameters:
      bitsize - bitsize [0, 64]
      Returns:
      the mask
      Throws:
      IllegalArgumentException - if the bitsize is out of range
    • makeInverseMask

      public static long makeInverseMask(int bitsize)
      Create a 64-bit inverse mask. Safe replacement for ~((ONE << BITSIZE) - ONE) constructs.
      Parameters:
      bitsize - bitsize [0, 64]
      Returns:
      the inverse mask
      Throws:
      IllegalArgumentException - if the bitsize is out of range
    • makeOverflow

      public static long makeOverflow(int bitsize)
      Create a 64-bit overflow. Safe replacement for (ONE << BITSIZE) constructs.
      Parameters:
      bitsize - bitsize [0, 64]
      Returns:
      the overflow
      Throws:
      IllegalArgumentException - if the bitsize is out of range
    • betweenExclusive

      public static boolean betweenExclusive(long value, long min, long max)
      Check that a value is between two bounds (exclusive).
      Parameters:
      value - value
      min - min bound
      max - max bound
      Returns:
      true if when value is in range ]min, max[
    • betweenInclusive

      public static boolean betweenInclusive(long value, long min, long max)
      Check that a value is between two bounds (inclusive).
      Parameters:
      value - value
      min - min bound
      max - max bound
      Returns:
      true if when value is in range [min, max]
    • signExtend32

      public static long signExtend32(long value, int bitsize)
      Sign-extend a value to 32 bits.
      Parameters:
      value - value to extend
      bitsize - must be in [1, 32]
      Returns:
      the sign-extended 32-bit value stored in a long
    • signExtend32

      public static int signExtend32(int value, int bitsize)
      Sign-extend a value to 32 bits.
      Parameters:
      value - value to extend
      bitsize - must be in [1, 32]
      Returns:
      the sign-extended 32-bit value
    • signExtend

      public static long signExtend(long value, int bitsize)
      Sign-extend a value to 64 bits.
      Parameters:
      value - value to extend
      bitsize - must be in [1, 64]
      Returns:
      the sign-extended 64-bit value
    • zeroExtend32

      public static long zeroExtend32(long value, int bitsize)
      Zero-extend a value to 32 bits.
      Parameters:
      value - value to extend
      bitsize - must be in [1, 32]
      Returns:
      the zero-extended 32-bit value stored in a long
    • zeroExtend

      public static long zeroExtend(long value, int bitsize)
      Zero-extend a value to 64 bits.
      Parameters:
      value - value to extend
      bitsize - must be in [1, 64]
      Returns:
      the zero-extended 64-bit value
    • msb

      public static int msb(long value, int bitsize)
      Retrieve the most significant bit of a fixed-width value.
      Parameters:
      value - value to inspect
      bitsize - must be in [1, 64]
      Returns:
      0 or 1
    • pow

      public static int pow(int a, int b)
      Integer exponentiation.
      Parameters:
      a - base
      b - exponent
      Returns:
      a**b (may have overflowed)
    • pow

      public static long pow(long a, long b)
      Long int exponentiation.
      Parameters:
      a - base
      b - exponent
      Returns:
      a**b (may have overflowed)
    • unmaskedShiftLeft

      public static long unmaskedShiftLeft(long v, int cnt)
      Left-shift a long value without Java's 6-bit shift-count masking.
      Parameters:
      v - value to shift
      cnt - shift count
      Returns:
      zero if cnt >= 64, otherwise v << cnt
    • unmaskedShiftRight

      public static long unmaskedShiftRight(long v, int cnt)
      Logically right-shift a long value without Java's 6-bit shift-count masking.
      Parameters:
      v - value to shift
      cnt - shift count
      Returns:
      zero if cnt >= 64, otherwise v >>> cnt
    • unmaskedArithShiftRight

      public static long unmaskedArithShiftRight(long v, int cnt)
      Arithmetically right-shift a long value without Java's 6-bit shift-count masking.
      Parameters:
      v - value to shift
      cnt - shift count
      Returns:
      sign-filled result if cnt >= 64, otherwise v >> cnt
    • moduloPositive

      public static int moduloPositive(int a, int b)
      Calculate the positive remainder of two integers.

      Note: Java standard modulo operators specifies that the remainder will have the sign of the numerator. This method provides an alternate way to calculate the remainder, often-time more useful, by making sure that it remains positive.

      Parameters:
      a - any integer
      b - a positive integer
      Returns:
      positive remainder
      Throws:
      ArithmeticException - if b is zero
    • avg

      public static double avg(Collection<? extends Number> collection)
      Compute the arithmetic mean of a collection of numbers.
      Parameters:
      collection - non-empty collection
      Returns:
      the average value
      Throws:
      IllegalArgumentException - if the collection is empty
    • avg

      public static double avg(byte[] array)
      Compute the arithmetic mean of a byte array.
      Parameters:
      array - non-empty array
      Returns:
      the average value
      Throws:
      IllegalArgumentException - if the array is empty
    • avg

      public static double avg(char[] array)
      Compute the arithmetic mean of a char array.
      Parameters:
      array - non-empty array
      Returns:
      the average value
      Throws:
      IllegalArgumentException - if the array is empty
    • avg

      public static double avg(short[] array)
      Compute the arithmetic mean of a short array.
      Parameters:
      array - non-empty array
      Returns:
      the average value
      Throws:
      IllegalArgumentException - if the array is empty
    • avg

      public static double avg(int[] array)
      Compute the arithmetic mean of an int array.
      Parameters:
      array - non-empty array
      Returns:
      the average value
      Throws:
      IllegalArgumentException - if the array is empty
    • avg

      public static double avg(long[] array)
      Compute the arithmetic mean of a long array.
      Parameters:
      array - non-empty array
      Returns:
      the average value
      Throws:
      IllegalArgumentException - if the array is empty
    • avg

      public static double avg(float[] array)
      Compute the arithmetic mean of a float array.
      Parameters:
      array - non-empty array
      Returns:
      the average value
      Throws:
      IllegalArgumentException - if the array is empty
    • avg

      public static double avg(double[] array)
      Compute the arithmetic mean of a double array.
      Parameters:
      array - non-empty array
      Returns:
      the average value
      Throws:
      IllegalArgumentException - if the array is empty
    • isPowerOfTwo

      public static boolean isPowerOfTwo(int n)
      Determine whether a strictly positive integer is a power of 2.
      Parameters:
      n - a positive integer
      Returns:
      true if n is a power of two
    • isPowerOfTwo

      public static boolean isPowerOfTwo(long n)
      Determine whether a strictly positive integer is a power of 2.
      Parameters:
      n - a positive integer
      Returns:
      true if n is a power of two
    • log2

      public static int log2(int n)
      Get the truncated base2 log of the input.
      Parameters:
      n - must be strictly positive
      Returns:
      the truncated base-2 logarithm of n
    • log2

      public static int log2(long n)
      Get the truncated base2 log of the input.
      Parameters:
      n - must be strictly positive
      Returns:
      the truncated base-2 logarithm of n
    • bitcount

      public static int bitcount(int value)
      Count the number of set bits in an integer.
      Parameters:
      value - value to inspect
      Returns:
      the number of one bits
    • bitcount

      public static int bitcount(long value)
      Count the number of set bits in a long integer.
      Parameters:
      value - value to inspect
      Returns:
      the number of one bits