Package com.pnfsoftware.jeb.util.math
Class MathUtil
java.lang.Object
com.pnfsoftware.jeb.util.math.MathUtil
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic 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) static double
avg
(char[] array) static double
avg
(double[] array) static double
avg
(float[] array) static double
avg
(int[] array) static double
avg
(long[] array) static double
avg
(short[] array) static double
avg
(Collection<? extends Number> collection) 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) static int
bitcount
(long value) static boolean
isPowerOfTwo
(int n) Determine is a strictly positive integer is a power of 2.static boolean
isPowerOfTwo
(long n) Determine is 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
minPositive
(int... values) Get the min of the values which is > 0.static int
moduloPositive
(int a, int b) Calculate the positive remainder of two integers.static int
msb
(long value, int bitsize) 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) static int
signExtend32
(int value, int bitsize) static long
signExtend32
(long value, int bitsize) static long
unmaskedArithShiftRight
(long v, int cnt) static long
unmaskedShiftLeft
(long v, int cnt) static long
unmaskedShiftRight
(long v, int cnt) static long
zeroExtend
(long value, int bitsize) static long
zeroExtend32
(long value, int bitsize)
-
Constructor Details
-
MathUtil
public MathUtil()
-
-
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
-b
-- 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
-b
-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 OOR
-
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 OOR
-
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 OOR
-
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]
-
minPositive
public static int minPositive(int... values) Get the min of the values which is > 0. (for example when looking for min indexOf) -
signExtend32
public static long signExtend32(long value, int bitsize) - Parameters:
value
-bitsize
- must be in [0, 32]- Returns:
-
signExtend32
public static int signExtend32(int value, int bitsize) - Parameters:
value
-bitsize
- must be in [0, 32]- Returns:
-
signExtend
public static long signExtend(long value, int bitsize) - Parameters:
value
-bitsize
- must be in [0, 64]- Returns:
-
zeroExtend32
public static long zeroExtend32(long value, int bitsize) - Parameters:
value
-bitsize
- must be in [0, 32]- Returns:
-
zeroExtend
public static long zeroExtend(long value, int bitsize) - Parameters:
value
-bitsize
- must be in [0, 64]- Returns:
-
msb
public static int msb(long value, int bitsize) - Parameters:
value
-bitsize
- must be in [0, 64]- Returns:
-
pow
public static int pow(int a, int b) Integer exponentiation.- Parameters:
a
-b
-- Returns:
- a**b (may have overflowed)
-
pow
public static long pow(long a, long b) Long int exponentiation.- Parameters:
a
-b
-- Returns:
- a**b (may have overflowed)
-
unmaskedShiftLeft
public static long unmaskedShiftLeft(long v, int cnt) -
unmaskedShiftRight
public static long unmaskedShiftRight(long v, int cnt) -
unmaskedArithShiftRight
public static long unmaskedArithShiftRight(long v, int 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 strictly positive integer- Returns:
- positive remainder
-
avg
-
avg
public static double avg(byte[] array) -
avg
public static double avg(char[] array) -
avg
public static double avg(short[] array) -
avg
public static double avg(int[] array) -
avg
public static double avg(long[] array) -
avg
public static double avg(float[] array) -
avg
public static double avg(double[] array) -
isPowerOfTwo
public static boolean isPowerOfTwo(int n) Determine is a strictly positive integer is a power of 2.- Parameters:
n
- a positive integer- Returns:
-
isPowerOfTwo
public static boolean isPowerOfTwo(long n) Determine is a strictly positive integer is a power of 2.- Parameters:
n
- a positive integer- Returns:
-
log2
public static int log2(int n) Get the truncated base2 log of the input.- Parameters:
n
- must be strictly positive- Returns:
-
log2
public static int log2(long n) Get the truncated base2 log of the input.- Parameters:
n
- must be strictly positive- Returns:
-
bitcount
public static int bitcount(int value) -
bitcount
public static int bitcount(long value)
-