Package com.amalgamasimulation.utils
Class EpsilonComparator
java.lang.Object
com.amalgamasimulation.utils.EpsilonComparator
- All Implemented Interfaces:
Comparator<Double>
Class providing API for comparing
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.- Author:
- Andrey Malykhanov
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance ofEpsilonComparatorwith default epsilon value of 1E-7EpsilonComparator(double epsilon) Creates a new instance ofEpsilonComparatorwith the specified epsilon value. -
Method Summary
Modifier and TypeMethodDescriptionintbooleanbooleanequalTo(double a, double b) Checks if the two specified numbers are equal, or absolute difference between them is strictly less than epsilon, i.e.doubleReturns the current value of epsilon.inthashCode()booleanlessEqualThan(double a, double b) Checks if eithera <= b, or absolute difference betweenaandbis strictly less than epsilon, i.e.booleanlessThan(double a, double b) Checks thata < band absolute difference betweenaandbis not less than epsilon, i.e.booleanmoreEqualThan(double a, double b) Checks if eithera >= b, or absolute difference betweenaandbis strictly less than epsilon, i.e.booleanmoreThan(double a, double b) Checks thata > band absolute difference betweenaandbis not less than epsilon, i.e.voidsetEpsilon(double epsilon) Sets the new value of epsilon.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Comparator
reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Constructor Details
-
EpsilonComparator
public EpsilonComparator(double epsilon) Creates a new instance ofEpsilonComparatorwith the specified epsilon value. It is not recommended to set epsilon values greater than 0.01If
Double.NaN, infinite, or negative value is passed, throws aRuntimeException- Parameters:
epsilon- epsilon value
-
EpsilonComparator
public EpsilonComparator()Creates a new instance ofEpsilonComparatorwith default epsilon value of 1E-7
-
-
Method Details
-
getEpsilon
public double getEpsilon()Returns the current value of epsilon. The default value of epsilon is 1E-7- Returns:
- current value of epsilon
-
setEpsilon
public void setEpsilon(double epsilon) Sets the new value of epsilon. It is not recommended to set epsilon values greater than 0.01If
Double.NaN, infinite, or negative value is passed, throws aRuntimeException- Parameters:
epsilon- new epsilon value to be set
-
equalTo
public 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 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 description of
equalTo(double, double)method for the definition of equality.- Parameters:
a- first numberb- second number- Returns:
trueif eithera <= b, or absolute difference betweenaandbis strictly less than epsilon,falseotherwise
-
moreEqualThan
public boolean moreEqualThan(double a, double b) Checks if eithera >= b, or absolute difference betweenaandbis 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 eithera >= b, or absolute difference betweenaandbis strictly less than epsilon,falseotherwise
-
lessThan
public 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 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:
trueifa > band absolute difference betweenaandbis not less than epsilon,falseotherwise
-
compare
- Specified by:
comparein interfaceComparator<Double>
-
hashCode
public int hashCode() -
equals
- Specified by:
equalsin interfaceComparator<Double>- Overrides:
equalsin classObject
-