/*
 * This file is part of Mapyrus.
 *
 * Mapyrus 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.
 *
 * Mapyrus 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 Mapyrus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/*
 * @(#) $Id$
 */

package au.id.chenery.mapyrus;

/**
 * Provides ststic methods for solving numerical analysis problems
 * (problems with accuracy of floating point numbers).
 */
public class NumericalAnalysis
{
	/**
	 * Compares two floating numbers for equality with some tolerance.
	 * @param a first number
	 * @param b second number
	 * @return true if numbers are equal or very, very close to being equal.
	 */
	public static boolean equals(double a, double b)
	{
		boolean retval;
		double absA = Math.abs(a);
		double absB = Math.abs(b);
		double magnitude = Math.max(absA, absB);
		double diff = Math.abs(a - b);

		/*
		 * Values are equal if first twelve significant digits
		 * are the same.
		 */
		retval = (diff <= magnitude * 1e-12);
// TODO read my numerical analysis book to find better method
		return(retval);
	}

	/**
	 * Calculate a modulo b for real numbers.  For example,
	 * 6.7 modulo 2.2 is 0.1.
	 * @param a value to be divided.
	 * @param b divisor.
	 * @return a modulo b
	 */
	public static double fmod(double a, double b)
	{
		double retval = Math.IEEEremainder(a, b);
		if (retval < 0)
			retval += b;
		return(retval);
	}
}

@@ -971,24 +971,32 @@
 res two 
-floating
+double precision
  numbers
@@ -1382,18 +1382,111 @@
 %09 * 
-Values are
+Double precision numbers are accurate to 15-16%0A%09%09 * significant digits.%0A%09%09 *%0A%09%09 * Values are considered
  equ
@@ -1519,20 +1519,20 @@
 cant
+%0A%09%09 *
  digits
-%0A%09%09 *
  are
@@ -1670,16 +1670,657 @@
 val);%0A%09%7D
+%0A%09%0A%09/**%0A%09 * Compares two floating point numbers for equality with some tolerance.%0A%09 * @param a first number%0A%09 * @param b second number%0A%09 * @return true if numbers are equal or very, very close to being equal.%0A%09 */%0A%09public static boolean equals(float a, float b)%0A%09%7B%0A%09%09boolean retval;%0A%09%09float absA = Math.abs(a);%0A%09%09float absB = Math.abs(b);%0A%09%09float magnitude = Math.max(absA, absB);%0A%09%09float diff = Math.abs(a - b);%0A%0A%09%09/*%0A%09%09 * Floating point numbers are accurate to 7 significant digits.%0A%09%09 * %0A%09%09 * Values are considered equal if first five significant%0A%09%09 * digits are the same.%0A%09%09 */%0A%09%09retval = (diff %3C= magnitude * 1e-5);%0A%09%09return(retval);%0A%09%7D
 %0A%0A%09/**%0A%09

Add equals() method for float values too.