/**************************************************************************** ** COPYRIGHT (C): 1997 Cay S. Horstmann. All Rights Reserved. ** PROJECT: Practical OO Development with C++ and Java ** FILE: Approx.java ** PURPOSE: approximate floating-point equality ** VERSION 1.0 ** PROGRAMMERS: Cay Horstmann (CSH) ** RELEASE DATE: 3-15-97 (CSH) ** UPDATE HISTORY: ****************************************************************************/ package practicaloo; class Approx { public static boolean equal(double x, double y) /* PURPOSE: tests whether two floating point numbers are approximately equal RECEIVES: x, y - floating point numbers RETURNS: TRUE iff |x-y| approx. 0 */ { return Math.abs(x - y) / Math.max(Math.abs(x), Math.abs(y)) < EPS; } public static boolean lessOrEqual(double x, double y) /* PURPOSE: compares two floating point numbers, allowing for roundoff RECEIVES: x, y - floating point numbers RETURNS: TRUE iff x < y + roundoff */ { return (x - y) / Math.max(Math.abs(x), Math.abs(y)) < EPS; } private static final double EPS = 0.000001; }