package uk.ac.leeds.ccg.andyt.tests.java; import java.math.BigDecimal; import java.math.BigInteger; /* * TestPrimitive.java * * Created on 15 November 2001, 12:22 */ /** * * @author andyt * @version */ public class TestPrimitive { public TestPrimitive() {} 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[] ) { TestPrimitive tp = new TestPrimitive(); tp.run(); } public void run() { reportConsecutiveIntegerRangeForDoubles(); //testNull(); //testNaN(); //double tollerateMin = 1.0d; //double tollerateMax = 100.0d; //TestPrimitive t = new TestPrimitive( tollerateMin, tollerateMax ); } public void testNull() { Object objectNull = null; //System.out.println(" null.equals( null) " + objectNull.equals( null) ); System.out.println("hello"); System.out.println( "null != null " + ( null != null ) ); System.out.println( "null == null " + ( null == null ) ); System.out.println( "objectNull == null " + ( objectNull == null ) ); } public void testNaN() { double doubleNaN0 = Double.NaN; double doubleNaN1 = Double.NaN; if ( doubleNaN0 == doubleNaN1 ) { System.out.println("doubleNaN0 == doubleNaN1"); } if ( doubleNaN0 != doubleNaN1 ) { System.out.println("doubleNaN0 != doubleNaN1"); } if ( doubleNaN0 == 1.0d ) { System.out.println("doubleNaN0 == 1.0d"); } if ( doubleNaN0 != 1.0d ) { System.out.println("doubleNaN0 != 1.0d"); } } 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 ); } public void reportConsecutiveIntegerRangeForDoubles() { //double d0 = ( double ) Integer.MAX_VALUE; //double d0 = ( double ) ( Long.MAX_VALUE / 10000L ); //double d0 = ( double ) ( 9007199254740000L ); double d0 = ( double ) ( 9007199254000000L ); //double d0 = ( double ) ( 9007190000000000L ); //double d0 = ( double ) ( 9000000000000000L ); BigDecimal bd0; BigDecimal bd1 = new BigDecimal( d0 ); double di = 1.0d; BigDecimal bdi = new BigDecimal( di ); System.out.println( Integer.MAX_VALUE ); System.out.println( Long.MAX_VALUE ); try { do { d0 += di; bd0 = new BigDecimal( d0 ); bd1 = bd1.add( bdi ); //d0 += 1.0d; //bd0 = new BigDecimal( d0 ); //bd1 = bd1.add( one ); //System.out.println( bd1.toString() ); //System.out.println( Double.toString( d0 ) ); } while ( bd0.compareTo( bd1 ) == 0 ); } catch ( Exception e0 ) { e0.printStackTrace(); } System.out.println( "The smallest positive integer which cannot be precisely stored as a double is " + bd1.subtract( bdi ).toString() ); //System.out.println( "The largest double which is an integer is less than " + d0 ); d0 = ( double ) ( -9007199254000000L ); //double d0 = ( double ) ( 9007190000000000L ); //double d0 = ( double ) ( 9000000000000000L ); bd1 = new BigDecimal( d0 ); di = 1.0d; bdi = new BigDecimal( di ); System.out.println( Integer.MIN_VALUE ); System.out.println( Long.MIN_VALUE ); try { do { d0 -= di; bd0 = new BigDecimal( d0 ); bd1 = bd1.subtract( bdi ); //d0 += 1.0d; //bd0 = new BigDecimal( d0 ); //bd1 = bd1.add( one ); //System.out.println( bd1.toString() ); //System.out.println( Double.toString( d0 ) ); } while ( bd0.compareTo( bd1 ) == 0 ); } catch ( Exception e0 ) { e0.printStackTrace(); } System.out.println( "The largest negative integer which cannot be precisely stored as a double is " + bd1.add( bdi ).toString() ); //System.out.println( "The largest double which is an integer is less than " + d0 ); } }