Java Assert Equal assertEqual(final double d1, final double d2, final double precisionRange)

Here you can find the source of assertEqual(final double d1, final double d2, final double precisionRange)

Description

Asserts if the given two doubles are equal within the given precision range by the operation:
 Math.abs(d1 - d2) < precision 
.

License

Open Source License

Parameter

Parameter Description
d1 a double to check equality to the other given double.
d2 a double to check equality to the other given double.
precisionRange the range to allow differences of the two doubles without judging a difference - this is typically a small value below 0.5.

Return

true if both given doubles are equal within the given precision range.

Declaration

public static boolean assertEqual(final double d1, final double d2, final double precisionRange) 

Method Source Code

//package com.java2s;
/*/*from   ww w  . j  a va2s  .co  m*/
 *  MathUtil, utility class for math operations.
 *  Copyright (C) 2004 - 2011 Achim Westermann.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  If you modify or optimize the code in a useful way please let me know.
 *  Achim.Westermann@gmx.de
 *
 */

public class Main {
    /**
     * Asserts if the given two doubles are equal within the given precision
     * range by the operation:
     * 
     * <pre>
     * Math.abs(d1 - d2) &lt; precision
     * </pre>
     * 
     * .
     * <p>
     * 
     * Because floating point calculations may involve rounding, calculated
     * float and double values may not be accurate. This routine should be used
     * instead. If called with a very small precision range this routine will
     * not be stable against the rounding of calculated floats but at least
     * prevent a bug report of the findbugs tool. See the Java Language
     * Specification, section 4.2.4.
     * <p>
     * 
     * @param d1
     *            a double to check equality to the other given double.
     * 
     * @param d2
     *            a double to check equality to the other given double.
     * 
     * @param precisionRange
     *            the range to allow differences of the two doubles without
     *            judging a difference - this is typically a small value below
     *            0.5.
     * 
     * @return true if both given doubles are equal within the given precision
     *         range.
     */
    public static boolean assertEqual(final double d1, final double d2, final double precisionRange) {
        return Math.abs(d1 - d2) < precisionRange;
    }
}

Related

  1. assertApproxEquals(double a, double b, double relative, double absolute)
  2. assertArrayEquals(double[] p1, double[] p2, double eps)
  3. assertArrayLengthEqual(Object[] array, String name, Object[] array1, String name1)
  4. assertCharacterArraysEqual(char[] first, char[] second)
  5. assertDatesEqual(Calendar first, Calendar second)
  6. assertEqual(Object obj1, Object obj2)
  7. assertEquality(Object original, Object equal, Object... notEqual)
  8. assertEquals(double x, double y)
  9. assertEquals(double x, double y)