Checks if two doubles are approximately equal. - Java java.lang

Java examples for java.lang:double

Description

Checks if two doubles are approximately equal.

Demo Code

/*/* ww w .  ja va  2s  .co  m*/
 * Java-Math is a math library written for Java 1.6+ that is primarily 
 * intended to help with 2D and 3D graphics, physics, and other related
 * mathematical operations. 
 *  
 * Copyright (C) 2014  Alec Sobeck and Matthew Robertson
 *
 * Java-Math is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
//package com.java2s;

public class Main {
    /**
     * Checks if two doubles are approximately equal. This is done by seeing if the doubles are within 
     * 1E-6 of each other. 
     * @param a the first double to check for approximate equality
     * @param b the second double to check for approximate equality
     * @return a boolean, true, if the numbers are reasonably close together; otherwise false
     */
    public static boolean approximatelyEqual(double a, double b) {
        return (Math.abs(a - b) < 1e-6) ? true : false;
    }

    /**
     * Checks if two doubles are approximately equal. This is done by seeing if the doubles are within 
     * 1 * 10^-(allowedDecimals) of each other. 
     * @param a the first double to check for approximate equality
     * @param b the second double to check for approximate equality
     * @param allowedDecimals the number of decimal places allowed before comparing the two values. If this goes beyond
     * a certain value it may be impossible to compare equality as doubles can hold only a certain number of decimals.
     * @return a boolean, true, if the numbers are within the specified tolerance of each other; otherwise false
     */
    public static boolean approximatelyEqual(double a, double b,
            int allowedDecimals) {
        return (Math.abs(a - b) < Math.pow(10, -1 * allowedDecimals)) ? true
                : false;
    }

    /**
     * Checks if two floats are approximately equal. This is done by seeing if the floats are within 
     * 1E-6 of each other. 
     * @param a the first floats to check for approximate equality
     * @param b the second floats to check for approximate equality
     * @return a boolean, true, if the floats are reasonably close together; otherwise false
     */
    public static boolean approximatelyEqual(float a, float b) {
        return (Math.abs(a - b) < 1e-6) ? true : false;
    }

    /**
     * Checks if two floats are approximately equal. This is done by seeing if the floats are within 
     * 1 * 10^-(allowedDecimals) of each other. 
     * @param a the first floats to check for approximate equality
     * @param b the second floats to check for approximate equality
     * @param allowedDecimals the number of decimal places allowed before comparing the two values. If this goes beyond
     * a certain value it may be impossible to compare equality as floats can hold only a certain number of decimals.
     * @return a boolean, true, if the numbers are within the specified tolerance of each other; otherwise false
     */
    public static boolean approximatelyEqual(float a, float b,
            int allowedDecimals) {
        return (Math.abs(a - b) < Math.pow(10, -1 * allowedDecimals)) ? true
                : false;
    }
}

Related Tutorials