Here you can find the source of assertEqual(final double d1, final double d2, final double precisionRange)
Math.abs(d1 - d2) < precision.
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. |
public static boolean assertEqual(final double d1, final double d2, final double precisionRange)
//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) < 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; } }