Java Double Number Equal doubleEquality(double v1, double v2, double epsilon)

Here you can find the source of doubleEquality(double v1, double v2, double epsilon)

Description

a pseudo-equality check for doubles

License

Open Source License

Parameter

Parameter Description
v1 double no. 1
v2 double no. 2
epsilon an user-defined epsilon

Return

true, if they are pseudo-equal

Declaration

public static boolean doubleEquality(double v1, double v2, double epsilon) 

Method Source Code

//package com.java2s;
/*/*  w  w  w .j  a  va  2  s.c  om*/
 * The MIT License
 *
 * Copyright 2015 Hilmar.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    /**
     * the standard-epsilon for the double-equality check
     */
    private static double epsilonDoubleEq = 0.00001;

    /**
     * a pseudo-equality check for doubles
     *
     * @param v1 double no. 1
     * @param v2 double no. 2
     * @return true, if they are pseudo-equal
     */
    public static boolean doubleEquality(double v1, double v2) {
        return Math.abs(v1 - v2) < epsilonDoubleEq;
    }

    /**
     * a pseudo-equality check for doubles
     *
     * @param v1 double no. 1
     * @param v2 double no. 2
     * @param epsilon an user-defined epsilon
     * @return true, if they are pseudo-equal
     */
    public static boolean doubleEquality(double v1, double v2, double epsilon) {
        return Math.abs(v1 - v2) < epsilon;
    }
}

Related

  1. doubleEqual(double d1, double d2)
  2. doubleEqual(double d1, double d2, double delta)
  3. doubleEqual(double v1, double v2)
  4. doubleEquals(double a, double b)
  5. doubleEquals(double a, double b)
  6. doubleEquals(double a, double b)
  7. doubleEquals(double d1, double d2)