|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectedu.mines.jtk.util.Almost
public class Almost
This class allows safe comparisons of floating point numbers with limited precision. The Comparator interface should be used for instances of java.lang.Number.
| Field Summary | |
|---|---|
static Almost |
DOUBLE
This instance uses default double precision |
static Almost |
FLOAT
This instance uses default float precision |
| Constructor Summary | |
|---|---|
Almost()
Accept default precision, appropriate for arithmetic on floats. |
|
Almost(boolean isDouble)
Constructor that allows either double or float precision. |
|
Almost(double epsilon)
Specify precision to be used for operations. |
|
Almost(double epsilon,
double minValue)
Specify precision to be used for operations. |
|
Almost(int significantDigits)
Specify precision to be used for operations. |
|
| Method Summary | |
|---|---|
boolean |
between(double x,
double x1,
double x2)
See if value is between two other values, including almost equality |
int |
cmp(double r1,
double r2)
Check order of two numbers, within precision. |
int |
compare(java.lang.Number n1,
java.lang.Number n2)
|
double |
divide(double top,
double bottom,
boolean limitIsOne)
Safely divide one number by another. |
double |
divide(double top,
double bottom,
double limit)
Safely divide one number by another. |
boolean |
equal(double r1,
double r2)
See if two numbers are almost equal. |
boolean |
equals(java.lang.Object object)
|
boolean |
ge(double r1,
double r2)
Check whether first number is greater than or equal to second. |
double |
getEpsilon()
Get the smallest positive number that can be added to 1 before the number is considered different from 1. |
double |
getMinValue()
Get the smallest positive value that should be distinguished from zero. |
boolean |
gt(double r1,
double r2)
Check whether first number is strictly greater than second. |
int |
hashCode()
|
int |
hashCodeOf(java.lang.Number number)
Return a hashcode consistent with imprecise floating-point equality. |
int |
hashCodeOf(java.lang.Number number,
int significantDigits)
Return a hashcode consistent with imprecise floating-point equality. |
boolean |
le(double r1,
double r2)
Check whether first number is less than or equal to second. |
boolean |
lt(double r1,
double r2)
Check whether first number is strictly less than second. |
int |
outside(double x,
double x1,
double x2)
See if value is outside two other values. |
double |
reciprocal(double value)
Safely take the reciprocal of a number. |
java.lang.String |
toString()
|
boolean |
zero(double r)
See if the number is almost zero |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final Almost FLOAT
public static final Almost DOUBLE
| Constructor Detail |
|---|
public Almost()
public Almost(double epsilon,
double minValue)
epsilon - The smallest positive number that can be added to 1
before the number is considered different from 1.
For double precision, use a multiple of MathPlus.DBL_EPSILON.
For float precision use a multiple of MathPlus.FLT_EPSILON.
I recommend multiplying these values by at least 10
to allow for errors introduced by arithmetic.minValue - The smallest positive value that should be
distinguished from zero. Use a multiple (say 100) of
Double.MIN_VALUE or Float.MIN_VALUE.public Almost(double epsilon)
epsilon - The smallest positive number that can be added to 1
before the number is considered different from 1.
For float precision use a multiple of 1.192092896e-07.
For double precision, use a multiple of 2.2204460492503131e-016;
I recommend multiplying these values by at least 10
to allow for errors introduced by arithmetic.public Almost(int significantDigits)
significantDigits - public Almost(boolean isDouble)
isDouble - If true, the precision will be appropriate
for a double; if false, a float.| Method Detail |
|---|
public double getEpsilon()
public double getMinValue()
public boolean between(double x,
double x1,
double x2)
x - Value to checkx1 - Value at one end of interval.x2 - Value at other end of interval.
public int outside(double x,
double x1,
double x2)
x - Value to checkx1 - Value at one end of interval.x2 - Value at other end of interval.
public boolean zero(double r)
r - A number to check
public boolean equal(double r1,
double r2)
r1 - First number to checkr2 - Second number to check
public boolean lt(double r1,
double r2)
r1 - First number to checkr2 - Second number to check
public boolean le(double r1,
double r2)
r1 - First number to checkr2 - Second number to check
public boolean gt(double r1,
double r2)
r1 - First number to checkr2 - Second number to check
public boolean ge(double r1,
double r2)
r1 - First number to checkr2 - Second number to check
public int cmp(double r1,
double r2)
r1 - First number to checkr2 - Second number to check
public int hashCodeOf(java.lang.Number number,
int significantDigits)
number - Number to get hashcode ofsignificantDigits - Number of significant digits to honor in hashCode
public int hashCodeOf(java.lang.Number number)
number - number to get hashcode from
public double divide(double top,
double bottom,
boolean limitIsOne)
top - Numeratorbottom - DenominatorlimitIsOne - If true, 0/0 returns 1. If false, 0/0 returns 0.
public double reciprocal(double value)
value - Value to take reciprocal of.
public double divide(double top,
double bottom,
double limit)
top - Numeratorbottom - Denominatorlimit - This is the returned value for 0/0.
public int compare(java.lang.Number n1,
java.lang.Number n2)
compare in interface java.util.Comparator<java.lang.Number>public boolean equals(java.lang.Object object)
equals in interface java.util.Comparator<java.lang.Number>equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||