public final class MathUtils
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static int |
compare(double a,
double b,
double epsilon)
Compares two
Double numbers numerically, given the specified epsilon. |
static int |
compare(float a,
float b,
float epsilon)
Compares two
Float numbers numerically, given the specified epsilon. |
static boolean |
equals(double a,
double b,
double epsilon)
Compares two
Double numbers for equality, given the specified epsilon. |
static boolean |
equals(float a,
float b,
float epsilon)
Compares two
Float numbers for equality, given the specified epsilon. |
static <T> T |
getAny(java.util.Collection<T> collection)
Gets a random element from the specified
Collection. |
static <T> T |
getAny(java.util.List<T> list)
Gets a random element from the specified
List. |
static <T> T |
getAny(T[] array)
Gets a random element from the specified array.
|
static boolean |
isPrime(int candidate)
Determines whether the specified
Integer number is prime. |
static double |
normalize(double[] array)
Normalizes the specified array of non-negative
Double numbers. |
static float |
normalize(float[] array)
Normalizes the specified array of non-negative
Float numbers. |
static double |
restrict(double a,
double min,
double max)
Restricts the specified
Double number to the specified range. |
static float |
restrict(float a,
float min,
float max)
Restricts the specified
Float number to the specified range. |
static int |
restrict(int a,
int min,
int max)
Restricts the specified
Integer number to the specified range. |
static long |
restrict(long a,
long min,
long max)
Restricts the specified
Long number to the specified range. |
static int |
toIntExact(double value)
Casts the specified
Double value to Integer, throwing an exception on overflow. |
static int |
toIntExact(float value)
Casts the specified
Float value to Integer, throwing an exception on overflow. |
static long |
toLongExact(double value)
Casts the specified
Double value to Long, throwing an exception on overflow. |
static long |
toLongExact(float value)
Casts the specified
Float value to Long, throwing an exception on overflow. |
public static int compare(double a,
double b,
double epsilon)
Double numbers numerically, given the specified epsilon.a - the first Double number to compareb - the second Double number to compareepsilon - the maximum absolute difference where a and b are considered equala and b
is no greater than epsilon, otherwise a value less or greater
than zero if a is less or greater than b, respectivelyjava.lang.IllegalArgumentException - if epsilon is less than zeropublic static int compare(float a,
float b,
float epsilon)
Float numbers numerically, given the specified epsilon.a - the first Float number to compareb - the second Float number to compareepsilon - the maximum absolute difference where a and b are considered equala and b
is no greater than epsilon, otherwise a value less or greater
than zero if a is less or greater than b, respectivelyjava.lang.IllegalArgumentException - if epsilon is less than zeropublic static boolean equals(double a,
double b,
double epsilon)
Double numbers for equality, given the specified epsilon.a - the first Double number to compareb - the second Double number to compareepsilon - the maximum absolute difference where a and b are considered equaltrue if the absolute difference between a and b
is no greater than epsilon, else falsejava.lang.IllegalArgumentException - if epsilon is less than zeropublic static boolean equals(float a,
float b,
float epsilon)
Float numbers for equality, given the specified epsilon.a - the first Float number to compareb - the second Float number to compareepsilon - the maximum absolute difference where a and b are considered equaltrue if the absolute difference between a and b
is no greater than epsilon, else falsejava.lang.IllegalArgumentException - if epsilon is less than zeropublic static <T> T getAny(T[] array)
array element at a random index between zero
(inclusive) and length (exclusive). This is an O(1) operation.T - the type of all elements in the arrayarray - the array providing the elementsarrayjava.lang.IllegalArgumentException - if array is emptyjava.lang.NullPointerException - if array is nullpublic static <T> T getAny(java.util.Collection<T> collection)
Collection.
Iterates through collection for a random number of steps, between zero
(inclusive) to Collection.size() (exclusive). This is an O(n) operation.T - the type of all elements in the Collectioncollection - the Collection providing the elementscollectionjava.lang.IllegalArgumentException - if collection is emptyjava.lang.NullPointerException - if collection is nullpublic static <T> T getAny(java.util.List<T> list)
List.
Returns the list element at a random index between zero (inclusive)
and List.size() (exclusive). This is usually an O(1) operation.T - the type of all elements in the Listlist - the List providing the elementslistjava.lang.IllegalArgumentException - if list is emptyjava.lang.NullPointerException - if list is nullpublic static boolean isPrime(int candidate)
Integer number is prime.
Performs trial divisions of the specified candidate against
any number between two and the square root of candidate.candidate - the Integer number to examinetrue if candidate is prime, else falsejava.lang.IllegalArgumentException - if candidate is not positivepublic static double normalize(double[] array)
Double numbers.
Divides all array elements by the sum of all elements (which is also returned),
thus normalizing their values to a partitioning of the standard interval [0, 1].
If the sum of all elements is zero, all elements are instead set to 1.0/length
where length is the number of array elements.array - the array of non-negative Double numbers to normalizearray elements before normalizationjava.lang.NullPointerException - if array is nulljava.lang.IllegalArgumentException - if array contains a negative numberpublic static float normalize(float[] array)
Float numbers.
Divides all array elements by the sum of all elements (which is also returned),
thus normalizing their values to a partitioning of the standard interval [0, 1].
If the sum of all elements is zero, all elements are instead set to 1f/length
where length is the number of array elements.array - the array of non-negative Float numbers to normalizearray elements before normalizationjava.lang.NullPointerException - if array is nulljava.lang.IllegalArgumentException - if array contains a negative numberpublic static double restrict(double a,
double min,
double max)
Double number to the specified range.a - the Double number to restrictmin - the smallest permissible Double value for amax - the greatest permissible Double value for amin or max if a is less or greater than
min or max, respectively, otherwise a itselfpublic static float restrict(float a,
float min,
float max)
Float number to the specified range.a - the Float number to restrictmin - the smallest permissible Float value for amax - the greatest permissible Float value for amin or max if a is less or greater than
min or max, respectively, otherwise a itselfpublic static int restrict(int a,
int min,
int max)
Integer number to the specified range.a - the Integer number to restrictmin - the smallest permissible Integer value for amax - the greatest permissible Integer value for amin or max if a is less or greater than
min or max, respectively, otherwise a itselfpublic static long restrict(long a,
long min,
long max)
Long number to the specified range.a - the Long number to restrictmin - the smallest permissible Long value for amax - the greatest permissible Long value for amin or max if a is less or greater than
min or max, respectively, otherwise a itselfpublic static int toIntExact(double value)
Double value to Integer, throwing an exception on overflow.value - the Double value to convert to Integervalue to Integerjava.lang.ArithmeticException - if value overflows Integerpublic static int toIntExact(float value)
Float value to Integer, throwing an exception on overflow.value - the Float value to convert to Integervalue to Integerjava.lang.ArithmeticException - if value overflows Integerpublic static long toLongExact(double value)
Double value to Long, throwing an exception on overflow.value - the Double value to convert to Longvalue to Longjava.lang.ArithmeticException - if value overflows Longpublic static long toLongExact(float value)
Float value to Long, throwing an exception on overflow.value - the Float value to convert to Longvalue to Longjava.lang.ArithmeticException - if value overflows Long