Class DoubleComparator

java.lang.Object
es.urjc.etsii.grafo.util.DoubleComparator

public class DoubleComparator extends Object
Helper methods to perform comparisons between doubles (or floats) DANGER: DoubleComparator VIOLATES COMPARISON CONTRACT, DO NOT USE IN SORTS
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Default epsilon value.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    comparator(double d1, double d2)
    Compares the two specified double values.
    static int
    comparator(double d1, double d2, double eps)
    Compares the two specified double values.
    static boolean
    equals(double d1, double d2)
    Test two doubles for equality, uses default error margin
    static boolean
    equals(double d1, double d2, double eps)
    Test two doubles for equality
    static boolean
    isGreater(double d1, double d2)
    Check if the first double is stricly greater than the second
    static boolean
    isGreaterOrEquals(double d1, double d2)
    Check if the first double is greater than or equals the second
    static boolean
    isLess(double d1, double d2)
    Check if the first double is strictly smaller than the second
    static boolean
    isLessOrEquals(double d1, double d2)
    Check if the first double is smaller than or equals the second
    static boolean
    isNegative(double d1)
    Check if a given double has a negative value.
    static boolean
    isNegativeOrZero(double d1)
    Check if a given double has a negative value or equals zero.
    static boolean
    isPositive(double d1)
    Check if a given double has a positive value.
    static boolean
    isPositiveOrZero(double d1)
    Check if a given double has a positive value or equals to zero.
    static boolean
    isZero(double d1)
    Check if a given double equals zero.
    static void
    setPrecision(double epsilon)
    BE CAREFUL WITH THIS METHOD!

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_EPSILON

      public static final double DEFAULT_EPSILON
      Default epsilon value. If the difference between two numbers is less than epsilon, the numbers are considered equal.
      See Also:
  • Method Details

    • setPrecision

      public static void setPrecision(double epsilon)
      BE CAREFUL WITH THIS METHOD! AFFECTS ALL THREADS Change default comparator precision. Defaults to DEFAULT_EPSILON
      Parameters:
      epsilon - differences less than epsilon will not be counted as true differences. See the Wikipedia page for an intro on IEE754
    • equals

      public static boolean equals(double d1, double d2)
      Test two doubles for equality, uses default error margin
      Parameters:
      d1 - first double to test
      d2 - second double to test
      Returns:
      True, if the difference between them is less than 0.001%, false otherwise
    • equals

      public static boolean equals(double d1, double d2, double eps)
      Test two doubles for equality
      Parameters:
      d1 - first double to test
      d2 - second double to test
      eps - Error margin
      Returns:
      True, if the difference between them is less than the error margin, false otherwise
    • comparator

      public static int comparator(double d1, double d2, double eps)
      Compares the two specified double values. The sign of the integer value returned is the same as that of the integer that would be returned by the call:
          new Double(d1).compareTo(new Double(d2))
       
      Parameters:
      d1 - the first double to compare
      d2 - the second double to compare
      eps - epsilon to use in comparison
      Returns:
      the value 0 if d1 is numerically equal to d2; a value less than 0 if d1 is numerically less than d2; and a value greater than 0 if d1 is numerically greater than d2.
    • comparator

      public static int comparator(double d1, double d2)
      Compares the two specified double values. The sign of the integer value returned is the same as that of the integer that would be returned by the call:
          new Double(d1).compareTo(new Double(d2))
       
      Parameters:
      d1 - the first double to compare
      d2 - the second double to compare
      Returns:
      the value 0 if d1 is numerically equal to d2; a value less than 0 if d1 is numerically less than d2; and a value greater than 0 if d1 is numerically greater than d2.
    • isNegative

      public static boolean isNegative(double d1)
      Check if a given double has a negative value.
      Parameters:
      d1 - double to check
      Returns:
      true if d1 is strictly less than 0, false otherwise.
    • isNegativeOrZero

      public static boolean isNegativeOrZero(double d1)
      Check if a given double has a negative value or equals zero.
      Parameters:
      d1 - double to check
      Returns:
      true if d1 is less than or equal to 0, false otherwise.
    • isZero

      public static boolean isZero(double d1)
      Check if a given double equals zero.
      Parameters:
      d1 - double to check
      Returns:
      true if d1 equals 0, false otherwise.
    • isPositive

      public static boolean isPositive(double d1)
      Check if a given double has a positive value.
      Parameters:
      d1 - double to check
      Returns:
      true if d1 is strictly greater than 0, false otherwise.
    • isPositiveOrZero

      public static boolean isPositiveOrZero(double d1)
      Check if a given double has a positive value or equals to zero.
      Parameters:
      d1 - double to check
      Returns:
      true if d1 is greater than or equals to 0, false otherwise.
    • isGreater

      public static boolean isGreater(double d1, double d2)
      Check if the first double is stricly greater than the second
      Parameters:
      d1 - first double
      d2 - second double
      Returns:
      true if d1 > d2, false otherwise
    • isGreaterOrEquals

      public static boolean isGreaterOrEquals(double d1, double d2)
      Check if the first double is greater than or equals the second
      Parameters:
      d1 - first double
      d2 - second double
      Returns:
      true if d1 >= d2, false otherwise
    • isLess

      public static boolean isLess(double d1, double d2)
      Check if the first double is strictly smaller than the second
      Parameters:
      d1 - first double
      d2 - second double
      Returns:
      true if d1 > d2, false otherwise
    • isLessOrEquals

      public static boolean isLessOrEquals(double d1, double d2)
      Check if the first double is smaller than or equals the second
      Parameters:
      d1 - first double
      d2 - second double
      Returns:
      true if d1 <= d2, false otherwise