Check if two double precision numbers are "equal", i.e. - Android java.lang

Android examples for java.lang:Math

Description

Check if two double precision numbers are "equal", i.e.

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot.//  w w w.  j  ava 2  s  .c  o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;

public class Main {
    /**
     * Check if two double precision numbers are "equal", i.e. close enough
     * to a prespecified limit.
     * 
     * @param a
     *            First number to check
     * @param b
     *            Second number to check
     * @return True if the twho numbers are "equal", false otherwise
     */
    private static boolean equals(float a, float b) {
        return equals(a, b, 1.0e-5f);
    }

    /**
     * Check if two double precision numbers are "equal", i.e. close enough
     * to a given limit.
     * 
     * @param a
     *            First number to check
     * @param b
     *            Second number to check
     * @param limit
     *            The definition of "equal".
     * @return True if the twho numbers are "equal", false otherwise
     */
    private static boolean equals(float a, float b, float limit) {
        return Math.abs(a - b) < limit;
    }
}

Related Tutorials