Java examples for java.lang:double Format
Attempts to determine equality between any subclasses of number.
/*//from w w w . j a v a 2 s . c o 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. */ import java.math.BigDecimal; public class Main{ /** * Attempts to determine equality between any subclasses of number. This method is special because it attempts to account * for rounding errors in doubles and floats. Doubles are checked for equality with a call to * {@link #approximatelyEqual(double, double, int)} to 13 places and floats are checked for equality with a call to * {@link #approximatelyEqual(float, float)}. * <br> <br> * If the data types are not both doubles or floats then they will be compared for equality based on the rules of * a.equals(b). In general this will mean that variables with the same type and value are equal, but variables with * the same value but a different type will not be equal. IE 444L == 444L, but 444L != 444. * @param a a Number to compare for equality * @param b a Number to compare for equality * @return a boolean, true if the values are of the same type and value, but otherwise false */ public static boolean equal(Number a, Number b) { if (a instanceof Double && b instanceof Double) { return DecimalHelper.approximatelyEqual((Double) a, (Double) b, 13); } else if (a instanceof Float && b instanceof Float) { return DecimalHelper.approximatelyEqual((Float) a, (Float) b); } else { return a.equals(b); } } /** * 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; } }