Package org.eclipse.imagen.media.numeric
Class CompareOp
Object
CompareOp
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intacompare(double x1, double x2) Compares twodoublevalues using the default tolerance.static intacompare(double x1, double x2, double tol) Compares twodoublevalues using the specified tolerance.static intacompare(float x1, float x2) Compares twofloatvalues using the default tolerance.static intacompare(float x1, float x2, float tol) Compares twofloatvalues using the specified tolerance.static booleanaequal(double x1, double x2) Tests if twodoublevalues are equal within the default tolerance.static booleanaequal(double x1, double x2, double tol) Tests if twodoublevalues are equal within the specified tolerance.static booleanaequal(float x1, float x2) Tests if twofloatvalues are equal within the default tolerance.static booleanaequal(float x1, float x2, float tol) Tests if twofloatvalues are equal within the specified tolerance.static booleanisZero(double x) Tests if the givendoublevalue is within the default tolerance of zero.static booleanisZero(double x, double tol) Tests if the givendoublevalue is within the specified tolerance of zero.static booleanisZero(float x) Tests if the givenfloatvalue is within the default tolerance of zero.static booleanisZero(float x, float tol) Tests if the givenfloatvalue is within the specified tolerance of zero.static intpcompare(double x1, double x2, double propTol) Compares twodoublevalues using the specified proportional tolerance.static intpcompare(float x1, float x2, float propTol) Compares twofloatvalues using the specified proportional tolerance.static booleanpequal(double x1, double x2, double propTol) Tests if twodoublevalues are equal within the specified proportional tolerance.static booleanpequal(float x1, float x2, float propTol) Tests if twofloatvalues are equal within the specified proportional tolerance.
-
Field Details
-
DTOL
public static final double DTOLDefault tolerance for double comparisons: 1.0e-8- See Also:
-
FTOL
public static final float FTOLDefault 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 givendoublevalue is within the default tolerance of zero.- Parameters:
x- the value- Returns:
trueif zero;falseotherwise
-
isZero
public static boolean isZero(float x) Tests if the givenfloatvalue is within the default tolerance of zero.- Parameters:
x- the value- Returns:
trueif zero;falseotherwise
-
isZero
public static boolean isZero(double x, double tol) Tests if the givendoublevalue is within the specified tolerance of zero. Note that performance reasons,tolis assumed to be positive, ie. this is not checked.- Parameters:
x- the valuetol- the tolerance- Returns:
trueif zero;falseotherwise
-
isZero
public static boolean isZero(float x, float tol) Tests if the givenfloatvalue is within the specified tolerance of zero. Note that performance reasons,tolis assumed to be positive, ie. this is not checked.- Parameters:
x- the valuetol- the tolerance- Returns:
trueif zero;falseotherwise
-
acompare
public static int acompare(double x1, double x2) Compares twodoublevalues using the default tolerance.- Parameters:
x1- first valuex2- 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 twofloatvalues using the default tolerance.- Parameters:
x1- first valuex2- 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 twodoublevalues using the specified tolerance. Note that performance reasons,tolis assumed to be positive, ie. this is not checked.- Parameters:
x1- first valuex2- second valuetol- 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 twofloatvalues using the specified tolerance. Note that performance reasons,tolis assumed to be positive, ie. this is not checked.- Parameters:
x1- first valuex2- second valuetol- 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 twodoublevalues 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 valuex2- second valuepropTol- 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 twofloatvalues 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 valuex2- second valuepropTol- 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 twodoublevalues are equal within the default tolerance. This is equivalent todzero(x1 - x2).- Parameters:
x1- first valuex2- second value- Returns:
trueif equal;falseotherwise
-
aequal
public static boolean aequal(float x1, float x2) Tests if twofloatvalues are equal within the default tolerance. This is equivalent todzero(x1 - x2).- Parameters:
x1- first valuex2- second value- Returns:
trueif equal;falseotherwise
-
aequal
public static boolean aequal(double x1, double x2, double tol) Tests if twodoublevalues are equal within the specified tolerance. This is equivalent todzero(x1 - x2, tol). Note that performance reasons,tolis assumed to be positive, ie. this is not checked.- Parameters:
x1- first valuex2- second valuetol- comparison tolerance- Returns:
trueif equal;falseotherwise
-
aequal
public static boolean aequal(float x1, float x2, float tol) Tests if twofloatvalues are equal within the specified tolerance. This is equivalent todzero(x1 - x2, tol). Note that performance reasons,tolis assumed to be positive, ie. this is not checked.- Parameters:
x1- first valuex2- second valuetol- comparison tolerance- Returns:
trueif equal;falseotherwise
-
pequal
public static boolean pequal(double x1, double x2, double propTol) Tests if twodoublevalues 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 valuex2- second valuepropTol- proportional tolerance between 0 and 1- Returns:
trueif equal;falseotherwise
-
pequal
public static boolean pequal(float x1, float x2, float propTol) Tests if twofloatvalues 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 valuex2- second valuepropTol- proportional tolerance between 0 and 1- Returns:
trueif equal;falseotherwise
-