Class FuzzyFloat

java.lang.Object
org.plumelib.util.FuzzyFloat

public class FuzzyFloat extends Object
Routines for doing approximate ('fuzzy') floating-point comparisons. Those are comparisons that only require the floating-point numbers to be relatively close to one another to be equal, rather than exactly equal.

Floating-point numbers are compared for equality by dividing them by one another and comparing the ratio. By default, they must be within 0.0001 (0.01%) to be considered equal.

Zero is never considered equal to a non-zero number, no matter how small its value.

Two NaN floats are not considered equal (consistent with the == operator).

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    class 
    Lexically compares two double arrays.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a FuzzyFloat with the default relativeRatio value of .0001.
    FuzzyFloat(double relativeRatio)
    Creates a FuzzyFloat.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    eq(double d1, double d2)
    Test d1 and d2 for equality using the current ratio.
    boolean
    gt(double d1, double d2)
    test d1 and d2 for d1 > d2.
    boolean
    gte(double d1, double d2)
    test d1 and d2 for d1 >= d2.
    int
    indexOf(double[] a, double elt)
    Searches for the first occurrence of elt in a.
    int
    indexOf(double[] a, double[] sub)
    Searches for the first subsequence of a that matches sub elementwise.
    boolean
    isElemMatch(double[] a1, double[] a2)
    Determines whether or not a1 and a2 are set equivalent (contain only the same elements).
    boolean
    isSubset(double[] smaller, double[] bigger)
    Determines whether smaller is a subset of bigger.
    boolean
    lt(double d1, double d2)
    Test d1 and d2 for d1 < d2.
    boolean
    lte(double d1, double d2)
    Test d1 and d2 for d1 <= d2.
    boolean
    ne(double d1, double d2)
    Test d1 and d2 for non-equality using the current ratio.
    void
    setRelativeRatio(double relativeRatio)
    Set all the fields of this class.

    Methods inherited from class java.lang.Object

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

    • FuzzyFloat

      public FuzzyFloat()
      Creates a FuzzyFloat with the default relativeRatio value of .0001.
    • FuzzyFloat

      public FuzzyFloat(double relativeRatio)
      Creates a FuzzyFloat. Specify the specific relative difference allowed between two floats in order for them to be equal. The default is 0.0001. A relative diff of zero, disables it (i.e., this class's methods work just like regular Java arithmetic comparisons).
      Parameters:
      relativeRatio - the relative diff to use; see setRelativeRatio(double)
  • Method Details

    • setRelativeRatio

      public void setRelativeRatio(@UnknownInitialization FuzzyFloat this, double relativeRatio)
      Set all the fields of this class.
      Parameters:
      relativeRatio - the new relative diff to use; a number near zero (zero is also permitted, which requires exact matching rather than permitting fuzzy matching)
      See Also:
    • eq

      @Pure public boolean eq(double d1, double d2)
      Test d1 and d2 for equality using the current ratio. Two NaN floats are not considered equal (consistent with the == operator).

      Note that if one of the numbers if 0.0, then the other number must be less than the square of the fuzzy ratio. This policy accommodates roundoff errors in floating-point values.

      Parameters:
      d1 - the first value to compare
      d2 - the second value to compare
      Returns:
      true if d1 and d2 are considered equal, false otherwise
    • ne

      @Pure public boolean ne(double d1, double d2)
      Test d1 and d2 for non-equality using the current ratio.
      Parameters:
      d1 - the first value to compare
      d2 - the second value to compare
      Returns:
      whether d1 and d2 are non-equal
      See Also:
    • lt

      @Pure public boolean lt(double d1, double d2)
      Test d1 and d2 for d1 < d2. If d1 is equal to d2 using the current ratio this returns false.
      Parameters:
      d1 - the first value to compare
      d2 - the second value to compare
      Returns:
      whether d1 < d2
      See Also:
    • lte

      @Pure public boolean lte(double d1, double d2)
      Test d1 and d2 for d1 <= d2. If d1 is equal to d2 using the current ratio, this returns true.
      Parameters:
      d1 - the first value to compare
      d2 - the second value to compare
      Returns:
      whether d1 ≤ d2
      See Also:
    • gt

      @Pure public boolean gt(double d1, double d2)
      test d1 and d2 for d1 > d2. IF d1 is equal to d2 using the current ratio, this returns false.
      Parameters:
      d1 - the first value to compare
      d2 - the second value to compare
      Returns:
      whether d1 > d2
      See Also:
    • gte

      @Pure public boolean gte(double d1, double d2)
      test d1 and d2 for d1 >= d2. If d1 is equal to d2 using the current ratio, this returns true.
      Parameters:
      d1 - the first value to compare
      d2 - the second value to compare
      Returns:
      whether d1 ≥ d2
      See Also:
    • indexOf

      @Pure public int indexOf(double[] a, double elt)
      Searches for the first occurrence of elt in a. elt is considered equal to a[i] if it passes the eq(double, double) test.
      Parameters:
      a - the array to search
      elt - the element to search for
      Returns:
      the first index containing the specified element, or -1 if the element is not found in the array
      See Also:
    • indexOf

      @Pure public int indexOf(double[] a, double[] sub)
      Searches for the first subsequence of a that matches sub elementwise. Elements of sub are considered to match elements of a if they pass the eq(double, double) test.
      Parameters:
      a - the sequence to search in
      sub - the sequence to search for
      Returns:
      the first index whose subarray is equal to the specified array or -1 if no such subarray is found in the array
      See Also:
    • isElemMatch

      @Pure public boolean isElemMatch(double[] a1, double[] a2)
      Determines whether or not a1 and a2 are set equivalent (contain only the same elements). Element comparison uses eq(double, double).

      Note that this implementation is optimized for cases where the elements are actually the same, since it does a sort of both arrays before starting the comparisons.

      Parameters:
      a1 - the first set to compare
      a2 - the second set to compare
      Returns:
      true if a1 and a2 are set equivalent, false otherwise
    • isSubset

      @Pure public boolean isSubset(double[] smaller, double[] bigger)
      Determines whether smaller is a subset of bigger. Element comparison uses eq(double, double).

      Note that this implementation is optimized for cases where the elements are actually the same, since it does a sort of both arrays before starting the comparisons.

      Parameters:
      smaller - the possibly-smaller subset
      bigger - the possibly-larger set
      Returns:
      true if smaller is a subset (each element of smaller is also a element of bigger) of bigger, false otherwise