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  

Data Type Double:
  1. Cast double to integer
  2. Create Double from double value
  3. Compare two double type variables within epsilon
  4. Compare double value arrays for almost equal
  5. Convert double to string
  6. Convert Double to numeric primitive data types
  7. Format double to percentage
  8. Is Double Infinite
  9. Is double positive infinity
  10. Is Double Not a Number(NaN)
  11. Round a double using BigDecimal
  12. Round double half up
  13. Min and Max value fo double type