Class Compare
Double values
considering the specified comparison precision. The precision is defined by
so-called epsilon value. Two values are considered equal if absolute
difference between them is less than epsilon. This value can be set using
setEpsilon(double) static method of this class. The default value of
epsilon is 1E-7.
All comparisons are performed with the default instance of
EpsilonComparator. For most uses, the only instance of
EpsilonComparator per program is sufficient, so this Compare
class serves as a wrapper around a singleton instance of epsilon comparator.
If comparisons with several different precisions is required, several
instances of EpsilonComparator can be used instead of this class.
- Author:
- Andrey Malykhanov
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanstatic booleanbeforeThan(LocalTime a, LocalTime b) static booleanequalTo(double a, double b) Checks if the two specified numbers are equal, or absolute difference between them is strictly less than epsilon, i.e.,a == b || Math.abs(a - b) < epsilon.static booleanstatic EpsilonComparatorReturns the default instance ofEpsilonComparatorthat actually performs all the comparisons.static doubleReturns the current epsilon value.static com.amalgamasimulation.utils.EpsilonLocalTimeComparatorstatic doublestatic booleanlessEqualThan(double a, double b) Checks if eithera <= b, or absolute difference betweenaandbis strictly less than epsilon, i.e.,a <= b || equalTo(a, b).static booleanlessThan(double a, double b) Checks thata < band absolute difference betweenaandbis not less than epsilon, i.e.static booleanmoreEqualThan(double a, double b) Checks if either a >= b, or absolute difference between a and b is strictly less than epsilon, i.e.static booleanmoreThan(double a, double b) Checks thata > band absolute difference betweenaandbis not less than epsilon, i.e.static voidsetEpsilon(double epsilon) Sets the new value of epsilon.static voidsetTimeEpsilon(double epsilon)
-
Method Details
-
getEpsilon
public static double getEpsilon()Returns the current epsilon value. This value is used to perform all comparisons in static methods of this class. The default value of epsilon is1E-7.- Returns:
- current epsilon value
-
getComparator
Returns the default instance ofEpsilonComparatorthat actually performs all the comparisons.- Returns:
- default instance of
EpsilonComparator
-
setEpsilon
public static void setEpsilon(double epsilon) Sets the new value of epsilon. It is not recommended to set epsilon values greater than 0.01- Parameters:
epsilon- new epsilon value to be set- Throws:
RuntimeException- ifDouble.NaN, infinite, or negative value is passed
-
equalTo
public static boolean equalTo(double a, double b) Checks if the two specified numbers are equal, or absolute difference between them is strictly less than epsilon, i.e.,a == b || Math.abs(a - b) < epsilon. The current epsilon can be retrieved bygetEpsilon()method.Infinite values with the same sign are considered equal
If either of the numbers is
Double.NaN, returnsfalse- Parameters:
a- first numberb- second number- Returns:
trueif the two specified numbers are equal or absolute difference between them is less than epsilon,falseotherwise
-
lessEqualThan
public static boolean lessEqualThan(double a, double b) Checks if eithera <= b, or absolute difference betweenaandbis strictly less than epsilon, i.e.,a <= b || equalTo(a, b).See the description of
equalTo(double, double)method for the definition of equality.- Parameters:
a- first numberb- second number- Returns:
trueif eithera <= b, or absolute difference between a and b is strictly less than epsilon,falseotherwise
-
moreEqualThan
public static boolean moreEqualThan(double a, double b) Checks if either a >= b, or absolute difference between a and b is strictly less than epsilon, i.e.a >= b || equalTo(a, b).See description of
equalTo(double, double)method for the definition of equality.- Parameters:
a- first numberb- second number- Returns:
trueif either a >= b, or absolute difference between a and b is strictly less than epsilon,falseotherwise
-
lessThan
public static boolean lessThan(double a, double b) Checks thata < band absolute difference betweenaandbis not less than epsilon, i.e.a < b && !equalTo(a, b).See description of
equalTo(double, double)method for the definition of equality.- Parameters:
a- first numberb- second number- Returns:
trueifa < band absolute difference betweenaandbis not less than epsilon,falseotherwise
-
moreThan
public static boolean moreThan(double a, double b) Checks thata > band absolute difference betweenaandbis not less than epsilon, i.e.a > b && !equalTo(a, b).See description of
equalTo(double, double)method for the definition of equality.- Parameters:
a- first numberb- second number- Returns:
trueif a > b and absolute difference betweenaandbis not less than epsilon,falseotherwise
-
getTimeEpsilon
public static double getTimeEpsilon() -
setTimeEpsilon
public static void setTimeEpsilon(double epsilon) -
getEpsilonLocalTimeComparator
public static com.amalgamasimulation.utils.EpsilonLocalTimeComparator getEpsilonLocalTimeComparator() -
equalTo
-
beforeThan
-
afterThan
-