public abstract class MathUtils extends Object
Modifier and Type | Method and Description |
---|---|
static boolean |
almostEqual(double a,
double b,
double delta)
Check whether two floating point values match with a given precision.
|
static int |
binarySearch(double[] a,
double key)
Perform a binary search on a sorted array
a to find the
element with the nearest element to key . |
static int |
binarySearchCeil(double[] a,
double key)
Perform a binary search on a sorted array
a to find the
element with the smallest distance to key . |
static int |
binarySearchFloor(double[] a,
double key)
Perform a binary search on a sorted array
a to find the
element with the smallest distance to key . |
static double |
ceil(double a,
double precision)
Returns a rounded number larger than
a with a defined
precision. |
static double |
floor(double a,
double precision)
Returns a rounded number smaller than
a with a defined
precision. |
static boolean |
isCalculatable(double n)
Returns whether a specified double can be used for calculations.
|
static boolean |
isCalculatable(Number n)
Returns whether a specified
java.lang.Number object can be
used for calculations. |
static double |
limit(double value,
double min,
double max)
Clamps a double number to specified limits: if
value is
greater than max then max will be returned. |
static float |
limit(float value,
float min,
float max)
Clamps a float number to specified limits: if
value is
greater than max then max will be returned. |
static int |
limit(int value,
int min,
int max)
Clamps a integer number to specified limits: if
value is
greater than max then max will be returned. |
static <T extends Number> |
limit(T value,
T min,
T max)
Clamps a number object to specified limits: if
value is
greater than max then max will be returned. |
static double |
magnitude(double base,
double n)
Returns the magnitude of the specified number.
|
static double |
normalizeDegrees(double angle)
Converts an angle in degrees so that it lies between 0.0 and 360.0.
|
static double |
quantile(List<Double> values,
double q)
Utility method used to calculate arbitrary quantiles from a sorted
list of values.
|
static <T extends Comparable<T>> |
randomizedSelect(List<T> a,
int lower,
int upper,
int i)
Perform a randomized search on an unsorted array
a to
find the ith smallest element. |
static double |
round(double a,
double precision)
Mathematically rounds a number with a defined precision.
|
public static boolean almostEqual(double a, double b, double delta)
a
- First valueb
- Second valuedelta
- Precisiontrue
if the difference of a and b is
smaller or equal than delta, otherwise false
public static double round(double a, double precision)
a
- Valueprecision
- Precisionpublic static double floor(double a, double precision)
a
with a defined
precision.a
- Valueprecision
- Precisionpublic static double ceil(double a, double precision)
a
with a defined
precision.a
- Valueprecision
- Precisionpublic static int binarySearch(double[] a, double key)
a
to find the
element with the nearest element to key
.a
- Array with ascending valueskey
- Pivot valuekey
public static int binarySearchFloor(double[] a, double key)
a
to find the
element with the smallest distance to key
. The returned
element's value is always less than or equal to key
.a
- Array with ascending valueskey
- Pivot valuekey
public static int binarySearchCeil(double[] a, double key)
a
to find the
element with the smallest distance to key
. The returned
element's value is always greater than or equal to key
.a
- Array with ascending valueskey
- Pivot valuekey
public static <T extends Number> T limit(T value, T min, T max)
value
is
greater than max
then max
will be returned.
If value
is greater than min
then
min
will be returned.T
- Numeric data typevalue
- Double value to be clampedmin
- Minimummax
- Maximumpublic static double limit(double value, double min, double max)
value
is
greater than max
then max
will be returned.
If value
is greater than min
then
min
will be returned.value
- Double value to be clampedmin
- Minimummax
- Maximumpublic static float limit(float value, float min, float max)
value
is
greater than max
then max
will be returned.
If value
is greater than min
then
min
will be returned.value
- Float value to be clampedmin
- Minimummax
- Maximumpublic static int limit(int value, int min, int max)
value
is
greater than max
then max
will be returned.
If value
is greater than min
then
min
will be returned.value
- Integer value to be clampedmin
- Minimummax
- Maximumpublic static <T extends Comparable<T>> int randomizedSelect(List<T> a, int lower, int upper, int i)
Perform a randomized search on an unsorted array a
to
find the ith smallest element. The array contents are be modified
during the operation!
See Cormen et al. (2001): Introduction to Algorithms. 2nd edition. p. 186
T
- Data type of the arraya
- Unsorted arraylower
- Starting indexupper
- End indexi
- Smallness rank of value to searchpublic static double magnitude(double base, double n)
Returns the magnitude of the specified number. Example for magnitude base 10:
-0.05 | -0.01 |
0.05 | 0.01 |
3.14 | 1.00 |
54.32 | 10.00 |
123.45 | 100.00 |
base
- Base.n
- Number.public static double quantile(List<Double> values, double q)
Utility method used to calculate arbitrary quantiles from a sorted list of values. Currently only one method is implemented: the default method that is used by R (method 7). The list must be sorted.
For more information see:
values
- Data values.q
- Quantile in range [0, 1]public static boolean isCalculatable(Number n)
java.lang.Number
object can be
used for calculations. null
values, NaN
values
or infinite values are considered as non-calculatable.n
- Number object.n
can be used for calculations.public static boolean isCalculatable(double n)
NaN
values or infinite values are considered
non-calculatable.n
- double valuen
can be used for calculations.public static double normalizeDegrees(double angle)
angle
- Arbitrary angle in degrees.Copyright © 2009-2013. All Rights Reserved.