Package com.pnfsoftware.jeb.util.math
Class MathUtil
java.lang.Object
com.pnfsoftware.jeb.util.math.MathUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanalmostEquals(double a, double b) Check for almost equality: tolerate a 1e-7 gap to make up for numerical errors inherent to IEEE754 calculations.static booleanalmostEquals(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 doubleavg(byte[] array) Compute the arithmetic mean of a byte array.static doubleavg(char[] array) Compute the arithmetic mean of a char array.static doubleavg(double[] array) Compute the arithmetic mean of a double array.static doubleavg(float[] array) Compute the arithmetic mean of a float array.static doubleavg(int[] array) Compute the arithmetic mean of an int array.static doubleavg(long[] array) Compute the arithmetic mean of a long array.static doubleavg(short[] array) Compute the arithmetic mean of a short array.static doubleavg(Collection<? extends Number> collection) Compute the arithmetic mean of a collection of numbers.static booleanbetweenExclusive(long value, long min, long max) Check that a value is between two bounds (exclusive).static booleanbetweenInclusive(long value, long min, long max) Check that a value is between two bounds (inclusive).static intbitcount(int value) Count the number of set bits in an integer.static intbitcount(long value) Count the number of set bits in a long integer.static booleanisPowerOfTwo(int n) Determine whether a strictly positive integer is a power of 2.static booleanisPowerOfTwo(long n) Determine whether a strictly positive integer is a power of 2.static intlog2(int n) Get the truncated base2 log of the input.static intlog2(long n) Get the truncated base2 log of the input.static longmakeInverseMask(int bitsize) Create a 64-bit inverse mask.static longmakeMask(int bitsize) Create a 64-bit mask.static longmakeOverflow(int bitsize) Create a 64-bit overflow.static intmoduloPositive(int a, int b) Calculate the positive remainder of two integers.static intmsb(long value, int bitsize) Retrieve the most significant bit of a fixed-width value.static intpow(int a, int b) Integer exponentiation.static longpow(long a, long b) Long int exponentiation.static longsignExtend(long value, int bitsize) Sign-extend a value to 64 bits.static intsignExtend32(int value, int bitsize) Sign-extend a value to 32 bits.static longsignExtend32(long value, int bitsize) Sign-extend a value to 32 bits.static longunmaskedArithShiftRight(long v, int cnt) Arithmetically right-shift a long value without Java's 6-bit shift-count masking.static longunmaskedShiftLeft(long v, int cnt) Left-shift a long value without Java's 6-bit shift-count masking.static longunmaskedShiftRight(long v, int cnt) Logically right-shift a long value without Java's 6-bit shift-count masking.static longzeroExtend(long value, int bitsize) Zero-extend a value to 64 bits.static longzeroExtend32(long value, int bitsize) Zero-extend a value to 32 bits.
-
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 valueb- 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 valueb- second valueepsilon- 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) - ONEconstructs.- 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- valuemin- min boundmax- 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- valuemin- min boundmax- 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 extendbitsize- 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 extendbitsize- 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 extendbitsize- 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 extendbitsize- 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 extendbitsize- 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 inspectbitsize- must be in [1, 64]- Returns:
- 0 or 1
-
pow
public static int pow(int a, int b) Integer exponentiation.- Parameters:
a- baseb- exponent- Returns:
- a**b (may have overflowed)
-
pow
public static long pow(long a, long b) Long int exponentiation.- Parameters:
a- baseb- 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 shiftcnt- shift count- Returns:
- zero if
cnt >= 64, otherwisev << 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 shiftcnt- shift count- Returns:
- zero if
cnt >= 64, otherwisev >>> 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 shiftcnt- shift count- Returns:
- sign-filled result if
cnt >= 64, otherwisev >> 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 integerb- a positive integer- Returns:
- positive remainder
- Throws:
ArithmeticException- if b is zero
-
avg
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
nis 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
nis 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
-