public final class NumberComparer
extends java.lang.Object
Double.compare(double, double)
won't do.Constructor and Description |
---|
NumberComparer() |
Modifier and Type | Method and Description |
---|---|
static int |
compare(double a,
double b)
This class attempts to reproduce Excel's behaviour for comparing numbers.
|
public static int compare(double a, double b)
Double.compare(double, double)
but with some
rounding. For numbers that are very close, this code converts to a format having 15
decimal digits of precision and a decimal exponent, before completing the comparison.
In Excel formula evaluation, expressions like "(0.06-0.01)=0.05" evaluate to "TRUE" even
though the equivalent java expression is false
. In examples like this,
Excel achieves the effect by having additional logic for comparison operations.
Note - Excel also gives special treatment to expressions like "0.06-0.01-0.05" which
evaluates to "0" (in java, rounding anomalies give a result of 6.9E-18). The special
behaviour here is for different reasons to the example above: If the last operator in a
cell formula is '+' or '-' and the result is less than 250 times smaller than
first operand, the result is rounded to zero.
Needless to say, the two rules are not consistent and it is relatively easy to find
examples that satisfynegative, 0, or positive
according to the standard Excel comparison
of values a and b.Copyright © 2005-2010 Potix Corporation. All Rights Reserved.