Class Compare

java.lang.Object
com.amalgamasimulation.utils.Compare

public abstract class Compare extends Object
Class providing static 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.

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 Type
    Method
    Description
    static boolean
     
    static boolean
     
    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.
    static boolean
     
    Returns the default instance of EpsilonComparator that actually performs all the comparisons.
    static double
    Returns the current epsilon value.
    static com.amalgamasimulation.utils.EpsilonLocalTimeComparator
     
    static double
     
    static 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).
    static boolean
    lessThan(double a, double b)
    Checks that a < b and absolute difference between a and b is not less than epsilon, i.e.
    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.
    static boolean
    moreThan(double a, double b)
    Checks that a > b and absolute difference between a and b is not less than epsilon, i.e.
    static void
    setEpsilon(double epsilon)
    Sets the new value of epsilon.
    static void
    setTimeEpsilon(double epsilon)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 is 1E-7.
      Returns:
      current epsilon value
    • getComparator

      public static EpsilonComparator getComparator()
      Returns the default instance of EpsilonComparator that 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 - if Double.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 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 static 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 the 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 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 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 static 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 static 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
    • getTimeEpsilon

      public static double getTimeEpsilon()
    • setTimeEpsilon

      public static void setTimeEpsilon(double epsilon)
    • getEpsilonLocalTimeComparator

      public static com.amalgamasimulation.utils.EpsilonLocalTimeComparator getEpsilonLocalTimeComparator()
    • equalTo

      public static boolean equalTo(LocalTime a, LocalTime b)
    • beforeThan

      public static boolean beforeThan(LocalTime a, LocalTime b)
    • afterThan

      public static boolean afterThan(LocalTime a, LocalTime b)