Class EpsilonComparator

java.lang.Object
com.amalgamasimulation.utils.EpsilonComparator
All Implemented Interfaces:
Comparator<Double>

public class EpsilonComparator extends Object implements 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 Details

    • EpsilonComparator

      public EpsilonComparator(double epsilon)
      Creates a new instance of EpsilonComparator with the specified epsilon value. It is not recommended to set epsilon values greater than 0.01

      If Double.NaN, infinite, or negative value is passed, throws a RuntimeException

      Parameters:
      epsilon - epsilon value
    • EpsilonComparator

      public EpsilonComparator()
      Creates a new instance of EpsilonComparator with 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.01

      If Double.NaN, infinite, or negative value is passed, throws a RuntimeException

      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 by getEpsilon() method.

      Infinite values with the same sign are considered equal

      If either of the numbers is Double.NaN, returns false

      Parameters:
      a - first number
      b - second number
      Returns:
      true if the two specified numbers are equal or absolute difference between them is less than epsilon, false otherwise
    • lessEqualThan

      public boolean lessEqualThan(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 number
      b - second number
      Returns:
      true if either a <= b, or absolute difference between a and b is strictly less than epsilon, false otherwise
    • moreEqualThan

      public 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 number
      b - second number
      Returns:
      true if either a >= b, or absolute difference between a and b is strictly less than epsilon, false otherwise
    • lessThan

      public boolean lessThan(double a, double b)
      Checks that a < b and absolute difference between a and b is 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 number
      b - second number
      Returns:
      true if a < b and absolute difference between a and b is not less than epsilon, false otherwise
    • moreThan

      public boolean moreThan(double a, double b)
      Checks that a > b and absolute difference between a and b is 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 number
      b - second number
      Returns:
      true if a > b and absolute difference between a and b is not less than epsilon, false otherwise
    • compare

      public int compare(Double o1, Double o2)
      Specified by:
      compare in interface Comparator<Double>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in interface Comparator<Double>
      Overrides:
      equals in class Object