public final class FloatingPointComparison extends Object
Modifier and Type | Method and Description |
---|---|
static double |
cubeRoot(double x)
Computes the real-valued cube root if a given number.
|
static double |
divideRoundTripIsGreaterOrEqual(double dividend,
double divisor)
Divides a dividend by a divisor such that if the quotient is later multiplied by the
divisor the result is greater than or equal to the dividend.
|
static double |
divideRoundTripIsLessOrEqual(double dividend,
double divisor)
Divides a dividend by a divisor such that if the quotient is later multiplied by the
divisor the result is less than or equal to the dividend.
|
static boolean |
equalsSignificantDigits(double x,
double y,
int significantDigits)
Compares two numbers up to the specified number of significant digits.
|
static boolean |
equalsSignificantDigits(double x,
double y,
int significantDigits,
int maxDigitDifference)
Compares two numbers up to the specified number of significant digits.
|
static boolean |
equalsSignificantDigitsOrAbsoluteError(double x,
double y,
int significantDigits,
double absoluteError)
Compares two numbers up to the specified number of significant digits or by their absolute error.
|
static int |
getVariableExponent(double var)
Returns the order N of the given variable of the form: var * 10^-(N+1) is less than 0.0
|
public static boolean equalsSignificantDigits(double x, double y, int significantDigits)
x
- The first number to compare.y
- The second number to comparesignificantDigits
- The number of significant digits to consider in the comparison.true
if the nth significant digit of the two numbers differ by no more than 1
and all preceding significant digits are equal; otherwise false
.public static boolean equalsSignificantDigits(double x, double y, int significantDigits, int maxDigitDifference)
x
- The first number to compare.y
- The second number to comparesignificantDigits
- The number of significant digits to consider in the comparison.maxDigitDifference
- The maximum difference in the last significant digit.true
if the nth significant digit of the two numbers differ by no more than 1
and all preceding significant digits are equal; otherwise false
.public static boolean equalsSignificantDigitsOrAbsoluteError(double x, double y, int significantDigits, double absoluteError)
x
- The first number to compare.y
- The second number to comparesignificantDigits
- The number of significant digits to consider in the comparison.absoluteError
- The maximum allowable difference between x and y.true
if the nth significant digit of the two numbers differ by no more than 1
and all preceding significant digits are equal, or if the difference between the two numbers is
less than the absolute error specified; otherwise false
.public static double divideRoundTripIsGreaterOrEqual(double dividend, double divisor)
If floating point numbers were infinitely precise, this method would be unnecessary. Since they're not, this method allows you to have control over how the error falls. For example, imagine that you have a floating point number that represents the minimum value allowed as input to a function. You want to express that minimum value in a different set of units such that the user can pass the minimum value to the function in that different set of units. By using this method to do the conversion to the alternate set of units, you guarantee that upon converting that value back to the original set of units the value will be greater than or equal to the original minimum value. Using a normal division might cause the value to actually be less than the minimum upon doing the round-trip conversion, which would likely cause the function to fail.
dividend
- The dividend.divisor
- The divisor.public static double divideRoundTripIsLessOrEqual(double dividend, double divisor)
If floating point numbers were infinitely precise, this method would be unnecessary. Since they're not, this method allows you to have control over how the error falls. For example, imagine that you have a floating point number that represents the maximum value allowed as input to a function. You want to express that maximum value in a different set of units such that the user can pass the maximum value to the function in that different set of units. By using this method to do the conversion to the alternate set of units, you guarantee that upon converting that value back to the original set of units the value will be less than or equal to the original maximum value. Using a normal division might cause the value to actually be greater than the maximum upon doing the round-trip conversion, which would likely cause the function to fail.
dividend
- The dividend.divisor
- The divisor.public static int getVariableExponent(double var)
var
- The variablepublic static double cubeRoot(double x)
x
- The value for which to compute the cube root.