public class CompareOp extends Object
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.| Modifier and Type | Field and Description |
|---|---|
static double |
DTOL
Default tolerance for double comparisons: 1.0e-8
|
static float |
FTOL
Default tolerance for float comparisons: 1.0e-4
|
| Constructor and Description |
|---|
CompareOp() |
| Modifier and Type | Method and 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. |
public static final double DTOL
public static final float FTOL
public static boolean isZero(double x)
double value is within the default tolerance
of zero.x - the valuetrue if zero; false otherwisepublic static boolean isZero(float x)
float value is within the default tolerance
of zero.x - the valuetrue if zero; false otherwisepublic static boolean isZero(double x,
double tol)
double value is within the specified tolerance
of zero. Note that performance reasons, tol is assumed
to be positive, ie. this is not checked.x - the valuetol - the tolerancetrue if zero; false otherwisepublic static boolean isZero(float x,
float tol)
float value is within the specified tolerance
of zero. Note that performance reasons, tol is assumed
to be positive, ie. this is not checked.x - the valuetol - the tolerancetrue if zero; false otherwisepublic static int acompare(double x1,
double x2)
double values using the default tolerance.x1 - first valuex2 - second valuepublic static int acompare(float x1,
float x2)
float values using the default tolerance.x1 - first valuex2 - second valuepublic static int acompare(double x1,
double x2,
double tol)
double values using the specified tolerance.
Note that performance reasons, tol is assumed
to be positive, ie. this is not checked.x1 - first valuex2 - second valuetol - comparison tolerancepublic static int acompare(float x1,
float x2,
float tol)
float values using the specified tolerance.
Note that performance reasons, tol is assumed
to be positive, ie. this is not checked.x1 - first valuex2 - second valuetol - comparison tolerancepublic static int pcompare(double x1,
double x2,
double propTol)
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);
x1 - first valuex2 - second valuepropTol - proportional tolerance between 0 and 1public static int pcompare(float x1,
float x2,
float propTol)
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);
x1 - first valuex2 - second valuepropTol - proportional tolerance between 0 and 1public static boolean aequal(double x1,
double x2)
double values are equal within the default tolerance.
This is equivalent to dzero(x1 - x2).x1 - first valuex2 - second valuetrue if equal; false otherwisepublic static boolean aequal(float x1,
float x2)
float values are equal within the default tolerance.
This is equivalent to dzero(x1 - x2).x1 - first valuex2 - second valuetrue if equal; false otherwisepublic static boolean aequal(double x1,
double x2,
double tol)
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.x1 - first valuex2 - second valuetol - comparison tolerancetrue if equal; false otherwisepublic static boolean aequal(float x1,
float x2,
float tol)
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.x1 - first valuex2 - second valuetol - comparison tolerancetrue if equal; false otherwisepublic static boolean pequal(double x1,
double x2,
double propTol)
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);
x1 - first valuex2 - second valuepropTol - proportional tolerance between 0 and 1true if equal; false otherwisepublic static boolean pequal(float x1,
float x2,
float propTol)
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);
x1 - first valuex2 - second valuepropTol - proportional tolerance between 0 and 1true if equal; false otherwiseCopyright © 2009–2018. All rights reserved.