public class Almost
extends java.lang.Object
implements java.io.Serializable, java.util.Comparator<java.lang.Number>
Modifier and Type | Field and Description |
---|---|
static Almost |
DOUBLE
This instance uses default double precision
|
static Almost |
FLOAT
This instance uses default float precision
|
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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
|
public static final Almost FLOAT
public static final Almost DOUBLE
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.
Should be much less than 1.public Almost(int significantDigits)
significantDigits
- the number of significant digits, should be
positive.public Almost(boolean isDouble)
isDouble
- If true, the precision will be appropriate
for a double; if false, a float.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 checkpublic boolean equal(double r1, double r2)
r1
- First number to checkr2
- Second number to checkpublic boolean lt(double r1, double r2)
r1
- First number to checkr2
- Second number to checkpublic boolean le(double r1, double r2)
r1
- First number to checkr2
- Second number to checkpublic boolean gt(double r1, double r2)
r1
- First number to checkr2
- Second number to checkpublic boolean ge(double r1, double r2)
r1
- First number to checkr2
- Second number to checkpublic int cmp(double r1, double r2)
r1
- First number to checkr2
- Second number to checkpublic int hashCodeOf(java.lang.Number number, int significantDigits)
number
- Number to get hashcode ofsignificantDigits
- Number of significant digits to honor in hashCodepublic int hashCodeOf(java.lang.Number number)
number
- number to get hashcode frompublic 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.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object