/* * TestPrimitive.java * * Created on 15 November 2001, 12:22 */ /** * * @author andyt * @version */ public class TestPrimitive { /** Creates new testPrimitives */ public TestPrimitive( double tollerateMin, double tollerateMax ) { System.out.println( "( int ) Double.NaN " + ( int ) Double.NaN ); System.out.println( "Double.MIN_VALUE " + Double.MIN_VALUE ); System.out.println( "Double.MAX_VALUE " + Double.MAX_VALUE ); System.out.println( "Double.NEGATIVE_INFINITY " + Double.NEGATIVE_INFINITY ); System.out.println( "Double.POSITIVE_INFINITY " + Double.POSITIVE_INFINITY ); System.out.println( "Double.TYPE " + Double.TYPE ); System.out.println( "Integer.MIN_VALUE " + Integer.MIN_VALUE ); System.out.println( "Integer.MAX_VALUE " + Integer.MAX_VALUE ); System.out.println( "Integer.TYPE " + Integer.TYPE ); System.out.println( "null " + null); double minTollerance = minTollerance( tollerateMin, tollerateMax ); System.out.println( "minTollerance " + minTollerance ); double maxTollerance = maxTollerance( tollerateMin, tollerateMax ); System.out.println( "maxTollerance " + maxTollerance ); } /** * @param args the command line arguments */ public static void main ( String args[] ) { double tollerateMin = 1.0d; double tollerateMax = 100.0d; TestPrimitive t = new TestPrimitive( tollerateMin, tollerateMax ); } public double tollerance( double d ) { double tollerance = d; double dummy = d; boolean calculated = false; while ( ! calculated ) { tollerance /= 10.0d; dummy = d + tollerance; if ( ! ( dummy > d ) ) { calculated = true; } } return tollerance * 10.0d; } /** * Returns the minimum double that can be added to a double in the range [ min, max ] such that * the result is larger than any double in the range. */ public double minTollerance( double min, double max ) { double tolleranceMin = min; double dummyMin = min; double tolleranceMax = min; double dummyMax = max; boolean calculated = false; while ( ! calculated ) { tolleranceMin /= 10.0d; tolleranceMax /= 10.0d; dummyMin = min + tolleranceMin; dummyMax = max + tolleranceMax; if ( ! ( dummyMin > min && dummyMax > max ) ) { calculated = true; } } return Math.min( tolleranceMin * 10.0d, tolleranceMax * 10 ); } /** * Returns the maximum double that can be added to a double in the range [ min, max ] such that * it can be taken away again and the same number returned */ public double maxTollerance( double min, double max ) { double tolleranceMin = min; double dummyMin = min; double tolleranceMax = max; double dummyMax = max; boolean calculated = false; while ( ! calculated ) { tolleranceMin *= 10.0d; tolleranceMax *= 10.0d; dummyMin += tolleranceMin; dummyMax += tolleranceMax; dummyMin -= tolleranceMin; dummyMax -= tolleranceMax; if ( dummyMin != min || dummyMax != max ) { calculated = true; } } return Math.max( tolleranceMin / 10.0d, tolleranceMax / 10 ); } }