public class

MathUtil

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

Class Overview

Set of common operations on Number that are not in Math.

Summary

Public Constructors
MathUtil()
Public Methods
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)
static double avg(float[] array)
static double avg(char[] array)
static double avg(short[] array)
static double avg(int[] array)
static double avg(long[] array)
static double avg(Collection<? extends Number> collection)
static double avg(double[] array)
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(long n)
Determine is a strictly positive integer is a power of 2.
static boolean isPowerOfTwo(int 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 long pow(long a, long b)
Long int exponentiation.
static int pow(int a, int b)
Integer exponentiation.
static long signExtend(long value, int bitsize)
static long signExtend32(long value, int bitsize)
static int signExtend32(int 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)
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public MathUtil ()

Public Methods

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.

Returns
  • true if |a-b|<=1e-7

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
epsilon a positive number
Returns
  • true if |a-b|<=epsilon

public static double avg (byte[] array)

public static double avg (float[] array)

public static double avg (char[] array)

public static double avg (short[] array)

public static double avg (int[] array)

public static double avg (long[] array)

public static double avg (Collection<? extends Number> collection)

public static double avg (double[] array)

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[

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]

public static int bitcount (int value)

public static int bitcount (long value)

public static boolean isPowerOfTwo (long n)

Determine is a strictly positive integer is a power of 2.

Parameters
n a positive integer

public static boolean isPowerOfTwo (int n)

Determine is a strictly positive integer is a power of 2.

Parameters
n a positive integer

public static int log2 (int n)

Get the truncated base2 log of the input.

Parameters
n must be strictly positive

public static int log2 (long n)

Get the truncated base2 log of the input.

Parameters
n must be strictly positive

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

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

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

public static int minPositive (int... values)

Get the min of the values which is > 0. (for example when looking for min indexOf)

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 strictly positive integer
Returns
  • positive remainder

public static int msb (long value, int bitsize)

Parameters
bitsize must be in [0, 64]

public static long pow (long a, long b)

Long int exponentiation.

Returns
  • a**b (may have overflowed)

public static int pow (int a, int b)

Integer exponentiation.

Returns
  • a**b (may have overflowed)

public static long signExtend (long value, int bitsize)

Parameters
bitsize must be in [0, 64]

public static long signExtend32 (long value, int bitsize)

Parameters
bitsize must be in [0, 32]

public static int signExtend32 (int value, int bitsize)

Parameters
bitsize must be in [0, 32]

public static long unmaskedArithShiftRight (long v, int cnt)

public static long unmaskedShiftLeft (long v, int cnt)

public static long unmaskedShiftRight (long v, int cnt)

public static long zeroExtend (long value, int bitsize)

Parameters
bitsize must be in [0, 64]

public static long zeroExtend32 (long value, int bitsize)

Parameters
bitsize must be in [0, 32]