Algorithm
Development Kit 1.0

algs.model
Class FloatingPoint

java.lang.Object
  extended by algs.model.FloatingPoint

public class FloatingPoint
extends java.lang.Object

Provides a standard API for evaluating double numbers when dealing with floating point rounding error.

Specifically, this class enables one to compare two double numbers to see if they are within an epsilon of each other.

Since:
1.0
Version:
1.0, 6/15/08
Author:
George Heineman

Field Summary
static double epsilon
          Numbers within this amount are considered to be the same.
 
Method Summary
static int compare(double d1, double d2)
          Return -1 if d1 < d2, 0 if d1 == d2, or +1 if d1 > d2.
static boolean greater(double x, double y)
          Given closeness-to-epsilon, is x > y?
static boolean greaterEquals(double x, double y)
          Given closeness-to-epsilon, is x >= y?
static boolean lesser(double x, double y)
          Given closeness-to-epsilon, is x < y?
static boolean lesserEquals(double x, double y)
          Given closeness-to-epsilon, is x <= y?
static boolean same(double d1, double d2)
          When value won't work, because numbers are potentially infinite, then use this one.
static double value(double x)
          See if the value is close enough to actually be considered 0.0 and return 0.0 if need be.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

epsilon

public static final double epsilon
Numbers within this amount are considered to be the same.

See Also:
Constant Field Values
Method Detail

compare

public static int compare(double d1,
                          double d2)
Return -1 if d1 < d2, 0 if d1 == d2, or +1 if d1 > d2.

Parameters:
d1 - first number against which to compare
d2 - second number being compared

same

public static boolean same(double d1,
                           double d2)
When value won't work, because numbers are potentially infinite, then use this one.

Standarard means for comparing double when dealing with the special quantities, NaN and infinite numbers. Also properly ensures that numbers "close to zero" (within an epsilon) are to be treated as zero for this computation.

Parameters:
d1 - first number being compared
d2 - second number being compared

value

public static double value(double x)
See if the value is close enough to actually be considered 0.0 and return 0.0 if need be.

Otherwise the value is returned.

Parameters:
x - value being considered

greaterEquals

public static boolean greaterEquals(double x,
                                    double y)
Given closeness-to-epsilon, is x >= y?


greater

public static boolean greater(double x,
                              double y)
Given closeness-to-epsilon, is x > y?


lesserEquals

public static boolean lesserEquals(double x,
                                   double y)
Given closeness-to-epsilon, is x <= y?


lesser

public static boolean lesser(double x,
                             double y)
Given closeness-to-epsilon, is x < y?


Algorithm Development Kit 1.0

This code supports the Algorithms in a Nutshell book, published by O'Reilly Media, Inc. in November 2008. Please visit the book web page to learn of any changes to the code repository or to record a potential defect.