org.apache.poi.ss.util
Class NumberComparer

java.lang.Object
  extended by org.apache.poi.ss.util.NumberComparer

public final class NumberComparer
extends java.lang.Object

Excel compares numbers using different rules to those of java, so Double.compare(double, double) won't do.

Author:
Josh Micich

Field Summary
static int BIASED_EXPONENT_SPECIAL_VALUE
          The value the exponent field gets for all NaN and Infinity values
static int EXPONENT_BIAS
           
static long FRAC_ASSUMED_HIGH_BIT
           
static long FRAC_MASK
           
 
Constructor Summary
NumberComparer()
           
 
Method Summary
static int compare(double a, double b)
          This class attempts to reproduce Excel's behaviour for comparing numbers.
static int getBiasedExponent(long rawBits)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FRAC_MASK

public static final long FRAC_MASK
See Also:
Constant Field Values

EXPONENT_BIAS

public static final int EXPONENT_BIAS
See Also:
Constant Field Values

FRAC_ASSUMED_HIGH_BIT

public static final long FRAC_ASSUMED_HIGH_BIT
See Also:
Constant Field Values

BIASED_EXPONENT_SPECIAL_VALUE

public static final int BIASED_EXPONENT_SPECIAL_VALUE
The value the exponent field gets for all NaN and Infinity values

See Also:
Constant Field Values
Constructor Detail

NumberComparer

public NumberComparer()
Method Detail

getBiasedExponent

public static int getBiasedExponent(long rawBits)
Parameters:
rawBits - the 64 bit binary representation of the double value
Returns:
the top 12 bits (sign and biased exponent value)

compare

public static int compare(double a,
                          double b)
This class attempts to reproduce Excel's behaviour for comparing numbers. Results are mostly the same as those from 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 satisfy
"A=B" is "TRUE" but "A-B" is not "0"
and
"A=B" is "FALSE" but "A-B" is "0"

This rule (for rounding the result of a final addition or subtraction), has not been implemented in POI (as of Jul-2009).

Returns:
negative, 0, or positive according to the standard Excel comparison of values a and b.