Class CompareOp

Object
CompareOp

public class CompareOp extends Object
Provides static methods to compare floating point values, taking into account an absolute or proportional tolerance. There are methods for both float and double values. The acompare and aequal methods use absolute tolerance while the pcompare and pequal methods use proportional tolerance.

For the proportional tolerance methods, a corresponding absolute tolerance is calculated as:


     atol = |ptol| * MAX(|x1|,|x2|)
 
Note: this class does not give any special consideration to the Float and Double constants NEGATIVE_INFINITY, POSITIVE_INFINITY and NaN over that provided by Java itself.
Since:
1.1
Author:
Michael Bedward
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Default tolerance for double comparisons: 1.0e-8
    static final float
    Default tolerance for float comparisons: 1.0e-4
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    acompare(double x1, double x2)
    Compares two double values using the default tolerance.
    static int
    acompare(double x1, double x2, double tol)
    Compares two double values using the specified tolerance.
    static int
    acompare(float x1, float x2)
    Compares two float values using the default tolerance.
    static int
    acompare(float x1, float x2, float tol)
    Compares two float values using the specified tolerance.
    static boolean
    aequal(double x1, double x2)
    Tests if two double values are equal within the default tolerance.
    static boolean
    aequal(double x1, double x2, double tol)
    Tests if two double values are equal within the specified tolerance.
    static boolean
    aequal(float x1, float x2)
    Tests if two float values are equal within the default tolerance.
    static boolean
    aequal(float x1, float x2, float tol)
    Tests if two float values are equal within the specified tolerance.
    static boolean
    isZero(double x)
    Tests if the given double value is within the default tolerance of zero.
    static boolean
    isZero(double x, double tol)
    Tests if the given double value is within the specified tolerance of zero.
    static boolean
    isZero(float x)
    Tests if the given float value is within the default tolerance of zero.
    static boolean
    isZero(float x, float tol)
    Tests if the given float value is within the specified tolerance of zero.
    static int
    pcompare(double x1, double x2, double propTol)
    Compares two double values using the specified proportional tolerance.
    static int
    pcompare(float x1, float x2, float propTol)
    Compares two float values using the specified proportional tolerance.
    static boolean
    pequal(double x1, double x2, double propTol)
    Tests if two double values are equal within the specified proportional tolerance.
    static boolean
    pequal(float x1, float x2, float propTol)
    Tests if two float values are equal within the specified proportional tolerance.

    Methods inherited from class Object

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

    • DTOL

      public static final double DTOL
      Default tolerance for double comparisons: 1.0e-8
      See Also:
    • FTOL

      public static final float FTOL
      Default tolerance for float comparisons: 1.0e-4
      See Also:
  • Constructor Details

    • CompareOp

      public CompareOp()
  • Method Details

    • isZero

      public static boolean isZero(double x)
      Tests if the given double value is within the default tolerance of zero.
      Parameters:
      x - the value
      Returns:
      true if zero; false otherwise
    • isZero

      public static boolean isZero(float x)
      Tests if the given float value is within the default tolerance of zero.
      Parameters:
      x - the value
      Returns:
      true if zero; false otherwise
    • isZero

      public static boolean isZero(double x, double tol)
      Tests if the given double value is within the specified tolerance of zero. Note that performance reasons, tol is assumed to be positive, ie. this is not checked.
      Parameters:
      x - the value
      tol - the tolerance
      Returns:
      true if zero; false otherwise
    • isZero

      public static boolean isZero(float x, float tol)
      Tests if the given float value is within the specified tolerance of zero. Note that performance reasons, tol is assumed to be positive, ie. this is not checked.
      Parameters:
      x - the value
      tol - the tolerance
      Returns:
      true if zero; false otherwise
    • acompare

      public static int acompare(double x1, double x2)
      Compares two double values using the default tolerance.
      Parameters:
      x1 - first value
      x2 - second value
      Returns:
      a value less than 0 if x1 is less than x2; 0 if x1 is equal to x2; a value greater than 0 if x1 is greater than x2
    • acompare

      public static int acompare(float x1, float x2)
      Compares two float values using the default tolerance.
      Parameters:
      x1 - first value
      x2 - second value
      Returns:
      a value less than 0 if x1 is less than x2; 0 if x1 is equal to x2; a value greater than 0 if x1 is greater than x2
    • acompare

      public static int acompare(double x1, double x2, double tol)
      Compares two double values using the specified tolerance. Note that performance reasons, tol is assumed to be positive, ie. this is not checked.
      Parameters:
      x1 - first value
      x2 - second value
      tol - comparison tolerance
      Returns:
      a value less than 0 if x1 is less than x2; 0 if x1 is equal to x2; a value greater than 0 if x1 is greater than x2
    • acompare

      public static int acompare(float x1, float x2, float tol)
      Compares two float values using the specified tolerance. Note that performance reasons, tol is assumed to be positive, ie. this is not checked.
      Parameters:
      x1 - first value
      x2 - second value
      tol - comparison tolerance
      Returns:
      a value less than 0 if x1 is less than x2; 0 if x1 is equal to x2; a value greater than 0 if x1 is greater than x2
    • pcompare

      public static int pcompare(double x1, double x2, double propTol)
      Compares two double values using the specified proportional tolerance. This is equivalent to:
      
           double absoluteTol = Math.abs(propTol) * Math.max(Math.abs(x1), Math.abs(x2));
           int comp = acompare(x1, x2, absTol);
       
      Parameters:
      x1 - first value
      x2 - second value
      propTol - proportional tolerance between 0 and 1
      Returns:
      a value less than 0 if x1 is less than x2; 0 if x1 is equal to x2; a value greater than 0 if x1 is greater than x2
    • pcompare

      public static int pcompare(float x1, float x2, float propTol)
      Compares two float values using the specified proportional tolerance. This is equivalent to:
      
           float absoluteTol = Math.abs(propTol) * Math.max(Math.abs(x1), Math.abs(x2));
           int comp = acompare(x1, x2, absTol);
       
      Parameters:
      x1 - first value
      x2 - second value
      propTol - proportional tolerance between 0 and 1
      Returns:
      a value less than 0 if x1 is less than x2; 0 if x1 is equal to x2; a value greater than 0 if x1 is greater than x2
    • aequal

      public static boolean aequal(double x1, double x2)
      Tests if two double values are equal within the default tolerance. This is equivalent to dzero(x1 - x2).
      Parameters:
      x1 - first value
      x2 - second value
      Returns:
      true if equal; false otherwise
    • aequal

      public static boolean aequal(float x1, float x2)
      Tests if two float values are equal within the default tolerance. This is equivalent to dzero(x1 - x2).
      Parameters:
      x1 - first value
      x2 - second value
      Returns:
      true if equal; false otherwise
    • aequal

      public static boolean aequal(double x1, double x2, double tol)
      Tests if two double values are equal within the specified tolerance. This is equivalent to dzero(x1 - x2, tol). Note that performance reasons, tol is assumed to be positive, ie. this is not checked.
      Parameters:
      x1 - first value
      x2 - second value
      tol - comparison tolerance
      Returns:
      true if equal; false otherwise
    • aequal

      public static boolean aequal(float x1, float x2, float tol)
      Tests if two float values are equal within the specified tolerance. This is equivalent to dzero(x1 - x2, tol). Note that performance reasons, tol is assumed to be positive, ie. this is not checked.
      Parameters:
      x1 - first value
      x2 - second value
      tol - comparison tolerance
      Returns:
      true if equal; false otherwise
    • pequal

      public static boolean pequal(double x1, double x2, double propTol)
      Tests if two double values are equal within the specified proportional tolerance. This is equivalent to:
      
           double absoluteTol = Math.abs(propTol) * Math.max(Math.abs(x1), Math.abs(x2));
           boolean b = aequal(x1, x2, absTol);
       
      Parameters:
      x1 - first value
      x2 - second value
      propTol - proportional tolerance between 0 and 1
      Returns:
      true if equal; false otherwise
    • pequal

      public static boolean pequal(float x1, float x2, float propTol)
      Tests if two float values are equal within the specified proportional tolerance. This is equivalent to:
      
           float absoluteTol = Math.abs(propTol) * Math.max(Math.abs(x1), Math.abs(x2));
           boolean b = aequal(x1, x2, absTol);
       
      Parameters:
      x1 - first value
      x2 - second value
      propTol - proportional tolerance between 0 and 1
      Returns:
      true if equal; false otherwise