Java Epsilon epsilonEquals(final double a, final double b, final double epsilon)

Here you can find the source of epsilonEquals(final double a, final double b, final double epsilon)

Description

Calculate whether the two scalars (a and b) are equal, given some slack value epsilon.

License

Open Source License

Parameter

Parameter Description
a first value to compare
b second value to compare
epsilon amount of difference allowed

Return

true if the values with epsilon, false otherwise

Declaration

public static boolean epsilonEquals(final double a, final double b, final double epsilon) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  w w.j  a  v a 2s  . c om
     * Calculate whether the two scalars (<tt>a</tt> and <tt>b</tt>) are equal, 
     * given some slack value <tt>epsilon</tt>. This allows floating point 
     * values to be compared for approximate equality, such that floating point
     * errors are alleviated.
     * 
     * @param a first value to compare
     * @param b second value to compare
     * @param epsilon amount of difference allowed
     * @return true if the values with epsilon, false otherwise
     */
    public static boolean epsilonEquals(final double a, final double b, final double epsilon) {
        final double diff = a - b;
        return diff < epsilon && diff > 0d - epsilon;
    }
}

Related

  1. epsilon(double v)
  2. epsilonCeiling(double value)
  3. epsilonEquals(final float x, final float y)
  4. epsilonEquals(float f1, float f2, float epsilon)
  5. epsilonEquals(float p_180185_0_, float p_180185_1_)
  6. epsilonEquals(float v1, float v2)