Checks the the given doubles if they are equals - which means they are closer than a predefined epsilon value, because comparing two doubles directly is a bad practice. - Java java.lang

Java examples for java.lang:double

Description

Checks the the given doubles if they are equals - which means they are closer than a predefined epsilon value, because comparing two doubles directly is a bad practice.

Demo Code


//package com.java2s;

public class Main {
    private static final double EPSILON = 0.0001;

    /**//from  w w  w . jav a  2s  . c om
     * Checks the the given doubles if they are equals - which means they are closer than a 
     * predefined epsilon value, because comparing two doubles directly is a bad practice.
     * @param a one of the values to be checked
     * @param b the other value to be checked
     * @return true if the two values are close enough to call them equals
     */
    public static boolean equalsEps(double a, double b) {
        return Math.abs(a - b) < EPSILON;
    }
}

Related Tutorials