Java examples for java.lang:double
Check if two doubles are equal to within specified tolerance.
/*//from w ww . j a v a2s. c o m * Copyright (c) 2014. Real Time Genomics Limited. * * Use of this source code is bound by the Real Time Genomics Limited Software Licence Agreement * for Academic Non-commercial Research Purposes only. * * If you did not receive a license accompanying this file, a copy must first be obtained by email * from support@realtimegenomics.com. On downloading, using and/or continuing to use this source * code you accept the terms of that license agreement and any amendments to those terms that may * be made from time to time by Real Time Genomics Limited. */ //package com.java2s; public class Main { /** * Check if two doubles are equal to within specified tolerance. * @param a parameter * @param b parameter * @param tolerance allowance for difference between a and b. * @return true iff (a ~= b) */ public static boolean approxEquals(final double a, final double b, final double tolerance) { if (Double.isNaN(a) || Double.isNaN(b)) { return false; } if (a == b) { return true; } if (Double.isInfinite(a) || Double.isInfinite(b)) { return false; } if (a >= (b - tolerance) && a <= (b + tolerance)) { return true; } return false; } }