Compare double value arrays for almost equal
public class Util {
public static boolean almostEquals(double[] d1, double[] d2, double eps) {
for (int i = 0; i < d1.length; i++) {
double v1 = d1[i];
double v2 = d2[i];
if (!almostEquals(v1, v2, eps))
return false;
}
return true;
}
/**
* Numbers that are closer than this are considered equal by almostEquals.
*/
public static double EPSILON = 0.000001;
public static boolean almostEquals(double d1, double d2) {
return almostEquals(d1, d2, EPSILON);
}
public static boolean almostEquals(double d1, double d2, double epsilon) {
return Math.abs(d1 - d2) < epsilon;
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Data Type Double:
- Cast double to integer
- Create Double from double value
- Compare two double type variables within epsilon
- Compare double value arrays for almost equal
- Convert double to string
- Convert Double to numeric primitive data types
- Format double to percentage
- Is Double Infinite
- Is double positive infinity
- Is Double Not a Number(NaN)
- Round a double using BigDecimal
- Round double half up
- Min and Max value fo double type