org.deri.iris.utils
Class StandardFloatingPointComparator

java.lang.Object
  extended by org.deri.iris.utils.StandardFloatingPointComparator
All Implemented Interfaces:
IFloatingPointComparator

public class StandardFloatingPointComparator
extends Object
implements IFloatingPointComparator

Utility class for floating point comparison. This class allows for round-off errors by using only the specified most significant bits of the operands, after allowing for scale.


Field Summary
static int BINARY_DIGITS_OF_PRECISION_DOUBLE
          ieee754 (double) uses 52 bits to represent the significand.
static int BINARY_DIGITS_OF_PRECISION_FLOAT
          ieee754 (float) uses 23 bits to represent the significand.
static int LEAST_SIGNIFICANT_BINARY_DIGITS_TO_IGNORE_DOUBLE
          The standard number of least significant bits to ignore for a double type.
static int LEAST_SIGNIFICANT_BINARY_DIGITS_TO_IGNORE_FLOAT
          The standard number of least significant bits to ignore for a float type.
 double MAX_DIFFERENCE_FROM_ONE
          This number is used to find the maximum difference between two numbers that can be considered logically the same.
static IFloatingPointComparator mStandardDoubleInstance
          The standard comparator for double type.
static IFloatingPointComparator mStandardFloatInstance
          The standard comparator for float types.
 
Constructor Summary
StandardFloatingPointComparator(int numberOfSignificantBits)
          Constructor.
 
Method Summary
 int compare(double a, double b)
          An error-safe comparison in the java style.
 boolean equals(double a, double b)
          Test two double values for equality.
static IFloatingPointComparator getDouble()
          Singleton-like method to get the standard setup for dealing with double type.
static IFloatingPointComparator getFloat()
          Singleton-like method to get the standard setup for dealing with float type.
 boolean greater(double a, double b)
          Floating-point error safe comparison.
 boolean greaterOrEquals(double a, double b)
          Floating-point error safe comparison.
 boolean isIntValue(double value)
          Indicates whether a double value contains an integer or a number very, very close to an integer.
 boolean less(double a, double b)
          Floating-point error safe comparison.
 boolean lessOrEquals(double a, double b)
          Floating-point error safe comparison.
 boolean notEquals(double a, double b)
          Floating-point error safe comparison.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_DIFFERENCE_FROM_ONE

public final double MAX_DIFFERENCE_FROM_ONE
This number is used to find the maximum difference between two numbers that can be considered logically the same. Any difference greater than this is considered significant and must therefore indicate different numbers. This number must be scaled as it indicates the maximum allowed difference from 1.0


BINARY_DIGITS_OF_PRECISION_DOUBLE

public static final int BINARY_DIGITS_OF_PRECISION_DOUBLE
ieee754 (double) uses 52 bits to represent the significand.

See Also:
Constant Field Values

BINARY_DIGITS_OF_PRECISION_FLOAT

public static final int BINARY_DIGITS_OF_PRECISION_FLOAT
ieee754 (float) uses 23 bits to represent the significand.

See Also:
Constant Field Values

LEAST_SIGNIFICANT_BINARY_DIGITS_TO_IGNORE_DOUBLE

public static final int LEAST_SIGNIFICANT_BINARY_DIGITS_TO_IGNORE_DOUBLE
The standard number of least significant bits to ignore for a double type.

See Also:
Constant Field Values

LEAST_SIGNIFICANT_BINARY_DIGITS_TO_IGNORE_FLOAT

public static final int LEAST_SIGNIFICANT_BINARY_DIGITS_TO_IGNORE_FLOAT
The standard number of least significant bits to ignore for a float type.

See Also:
Constant Field Values

mStandardDoubleInstance

public static final IFloatingPointComparator mStandardDoubleInstance
The standard comparator for double type.


mStandardFloatInstance

public static final IFloatingPointComparator mStandardFloatInstance
The standard comparator for float types.

Constructor Detail

StandardFloatingPointComparator

public StandardFloatingPointComparator(int numberOfSignificantBits)
Constructor.

Parameters:
numberOfSignificantBits - The number of significant bits in the significand to use when comparing values. If a non-standard setup is used then it is up to the user to set this value correctly.
Method Detail

getDouble

public static IFloatingPointComparator getDouble()
Singleton-like method to get the standard setup for dealing with double type.

Returns:
The standard double-configured FloatingPoint object.

getFloat

public static IFloatingPointComparator getFloat()
Singleton-like method to get the standard setup for dealing with float type.

Returns:
The standard float-configured FloatingPoint object.

compare

public int compare(double a,
                   double b)
Description copied from interface: IFloatingPointComparator
An error-safe comparison in the java style.

Specified by:
compare in interface IFloatingPointComparator
Parameters:
a - A double value
b - A double value
Returns:
-1 if a is significantly less than b, +1 if a is significantly greater than b, 0 if a and b are close enough.

less

public boolean less(double a,
                    double b)
Description copied from interface: IFloatingPointComparator
Floating-point error safe comparison.

Specified by:
less in interface IFloatingPointComparator
Parameters:
a - A double value
b - A double value
Returns:
true If a is less than b.

greater

public boolean greater(double a,
                       double b)
Description copied from interface: IFloatingPointComparator
Floating-point error safe comparison.

Specified by:
greater in interface IFloatingPointComparator
Parameters:
a - A double value
b - A double value
Returns:
true If a is greater than b.

greaterOrEquals

public boolean greaterOrEquals(double a,
                               double b)
Description copied from interface: IFloatingPointComparator
Floating-point error safe comparison.

Specified by:
greaterOrEquals in interface IFloatingPointComparator
Parameters:
a - A double value
b - A double value
Returns:
true If a is greater than or close enough to be equal to b.

lessOrEquals

public boolean lessOrEquals(double a,
                            double b)
Description copied from interface: IFloatingPointComparator
Floating-point error safe comparison.

Specified by:
lessOrEquals in interface IFloatingPointComparator
Parameters:
a - A double value
b - A double value
Returns:
true If a is less than or close enough to be equal to b.

equals

public boolean equals(double a,
                      double b)
Test two double values for equality. The two values are determined to be equal if they are bit-wise identical or their values lie very close together. In order to discover whether any difference between the two numbers is significant, a maximum allowed difference is calculated.

Specified by:
equals in interface IFloatingPointComparator
Parameters:
a - A double value
b - A double value
Returns:
true If a and b are exactly the same or close enough to being equal.

notEquals

public boolean notEquals(double a,
                         double b)
Description copied from interface: IFloatingPointComparator
Floating-point error safe comparison.

Specified by:
notEquals in interface IFloatingPointComparator
Returns:
true If a and b are significantly different.

isIntValue

public boolean isIntValue(double value)
Description copied from interface: IFloatingPointComparator
Indicates whether a double value contains an integer or a number very, very close to an integer.

Specified by:
isIntValue in interface IFloatingPointComparator
Parameters:
value - The value to test
Returns:
true If value holds an integer.