Check if two doubles are equal to machine precision. - Java java.lang

Java examples for java.lang:Math Operation

Description

Check if two doubles are equal to machine precision.

Demo Code

/**   Basic numeric operations not included in standard Java run-time library.
 *
 *   <p>/*from ww w .j ava  2s  .com*/
 *   The binomial methods are modified from those in the Colt library.
 *   </p>
 *
 *   <p>
 *   The following methods for trigonometric functions come from the
 *   Sfun class written by Visual Numerics.
 *   </p>
 *
 *   <ul>
 *   <li>acosh</li>
 *   <li>asinh</li>
 *   <li>atanh</li>
 *   <li>cot</li>
 *   <li>cosh</li>
 *   <li>sinh</li>
 *   <li>tanh</li>
 *   </ul>
 *
 *   <p>
 *   These methods are covered by the following license.
 *   </p>
 *
 * -------------------------------------------------------------------------
 *   $Id: Sfun.java,v 1.1.1.1 1999/03/05 21:43:39 brophy Exp $
 * -------------------------------------------------------------------------
 * Copyright (c) 1997 - 1998 by Visual Numerics, Inc. All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software is freely
 * granted by Visual Numerics, Inc., provided that the copyright notice
 * above and the following warranty disclaimer are preserved in human
 * readable form.
 *
 * Because this software is licensed free of charge, it is provided
 * "AS IS", with NO WARRANTY.  TO THE EXTENT PERMITTED BY LAW, VNI
 * DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO ITS PERFORMANCE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * VNI WILL NOT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING OUT OF THE USE
 * OF OR INABILITY TO USE THIS SOFTWARE, INCLUDING BUT NOT LIMITED TO DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, PUNITIVE, AND EXEMPLARY DAMAGES, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * -------------------------------------------------------------------------
 */

public class Main{
    public static void main(String[] argv) throws Exception{
        double a = 2.45678;
        double b = 2.45678;
        System.out.println(areEqual(a,b));
    }
    /**   Check if two doubles are equal to machine precision.
     *
     *   @param   a   First double.
     *   @param   b   Second double.
     *
     *   @return      True if a and b are equal to machine precision.
     */

    public static boolean areEqual(double a, double b) {
        return areEqual(a, b, Constants.MACHEPS);
    }
    /**   Check if two doubles are equal to specified tolerance.
     *
     *   @param   a         First double.
     *   @param   b         Second double.
     *   @param   tolerance   Tolerance.
     *
     *   @return            True if a and b are equal to specified tolerance.
     */

    public static boolean areEqual(double a, double b, double tolerance) {
        boolean result = (a == b);

        if (!result) {
            if (a == 0.0D) {
                result = (Math.abs(b) <= tolerance);
            } else if (b == 0.0D) {
                result = (Math.abs(a) <= tolerance);
            } else {
                result = (fuzzyCompare(a, b, tolerance) == 0);
            }
        }

        return result;
    }
    /**   Perform fuzzy comparison of two doubles with specified tolerance.
     *
     *   @param   a         First double.
     *   @param   b         Second double.
     *   @param   tolerance   Tolerance value.
     *
     *   @return            1 if a > b.
     *                  0 if a ~= b.
     *                  -1 if a < b.
     *
     *   <p>
     *   This is an implementation of an algorithm suggested by
     *   Donald E. Knuth in Section 4.2.2 of
     *   <em>Seminumerical Algorithms (3rd edition)</em>.
     *   </p>
     */

    public static int fuzzyCompare(double a, double b, double tolerance) {
        //   Check for exacty equality first
        //   to handle NaNs.

        if (a == b)
            return 0;

        //   Compute difference of a and b.

        double difference = a - b;

        //   Find exponent for whichever of a or b
        //   has largest absolute value.

        double maxAbs = Math.max(Math.abs(a), Math.abs(b));

        //   Get exponent.  Round up to next
        //   power of two.

        int exponent = new SplitDouble(maxAbs).exponent;

        //   Form neighborhood of size  2 * delta.

        double delta = tolerance * Math.pow(2, exponent);

        //   Assume a and b at least approximately
        //   equal.

        int result = 0;

        //   Return 1 if a > b and difference
        //   is outside delta neighborhood.

        if (difference > delta) {
            result = 1;
        }
        //   Return -1 if a < b and difference is
        //   outside delta neighborhood.

        else if (difference < -delta) {
            result = -1;
        }
        //   If difference lies between
        //   -delta and delta, a is exactly
        //   or approximately equal to b .
        //   Return 0 in this case.
        return result;
    }
}

Related Tutorials