// import becker.robots.*; // import java.util.Random; /** A class of code for testing arithmetic * * @author Duncan Buell * * written: 14 October 2007 * **/ public class TestCode { /* **************************************************************************** */ /** Put in a constructor just in case I want one later. **/ public TestCode() { } /* **************************************************************************** */ /** test the integer arithmetic size **/ public void doIntegerTest() { int theNumberBefore,theNumberAfter; System.out.println(""); System.out.println("Test of positive integer arithmetic sizes"); theNumberBefore = 1; theNumberAfter = 1; System.out.printf(" i Before After 2^i%n"); for(int i = 1; i <= 32; ++i) { theNumberAfter = theNumberBefore * 2; System.out.printf("%4d %13d %13d %5d%n",i,theNumberBefore,theNumberAfter,i); theNumberBefore = theNumberAfter; } // for(int i = 1; i <= 32; ++i) System.out.println(""); System.out.println("Test of negative integer arithmetic sizes"); System.out.printf(" i Before After 2^i%n"); theNumberBefore = -1; theNumberAfter = -1; for(int i = 1; i <= 32; ++i) { theNumberAfter = theNumberBefore * 2; System.out.printf("%4d %13d %13d %5d%n",i,theNumberBefore,theNumberAfter,i); theNumberBefore = theNumberAfter; } // for(int i = 1; i <= 32; ++i) } // public void doIntegerTest(); /* **************************************************************************** */ /** test the double precision point arithmetic size * all this shows is that doubles are not like integers **/ public void doDoubleTest1() { double theNumberBefore,theNumberAfter; System.out.println(""); System.out.println("Test 1 of positive double precision arithmetic sizes"); System.out.printf(" i Before After 2.0^i%n"); theNumberBefore = 1.0; theNumberAfter = 1.0; for(int i = 1; i <= 32; ++i) { theNumberAfter = theNumberBefore * 2.0; System.out.printf("%4d %18f %18f %5d%n",i,theNumberBefore,theNumberAfter,i); theNumberBefore = theNumberAfter; } // for(int i = 1; i <= 32; ++i) System.out.println(""); System.out.println("Test 1 of negative double precision arithmetic sizes"); System.out.printf(" i Before After 2.0^i%n"); theNumberBefore = -1.0; theNumberAfter = -1.0; for(int i = 1; i <= 32; ++i) { theNumberAfter = theNumberBefore * 2.0; System.out.printf("%4d %18f %18f %5d%n",i,theNumberBefore,theNumberAfter,i); theNumberBefore = theNumberAfter; } // for(int i = 1; i <= 32; ++i) } // public void doDoubleTest1(); /* **************************************************************************** */ /** test the double precision point arithmetic size * this test demonstrates double precision sizes **/ public void doDoubleTest2() { double theNumberBefore,theNumberAfter; System.out.println(""); System.out.println("Test 2 of double precision arithmetic sizes"); theNumberBefore = 2.0; theNumberAfter = 2.0; for(int i = 1; i <= 12; ++i) { theNumberAfter = theNumberBefore * theNumberBefore; System.out.println("i=" + i + " Before=" + theNumberBefore + ", After=" + theNumberAfter + " is Before^2"); System.out.printf("%4d %18f %18f %5d%n",i,theNumberBefore,theNumberAfter,i); theNumberBefore = theNumberAfter; } // for(int i = 1; i <= 12; ++i) } // public void doDoubleTest2(); /* **************************************************************************** */ /** test the double precision point arithmetic size * this test demonstrates double precision sizes **/ public void doDoubleTest3() { double theAddin, theBaseNumber,theSum; System.out.println(""); System.out.println("Test 3 of double precision arithmetic sizes"); theBaseNumber= 1.0; theAddin = 1.0; for(int i = 1; i <= 53; ++i) { theAddin = theAddin / 2.0; theSum = theBaseNumber + theAddin; // System.out.printf("%4d %18f %18f %18f%n",i,theBaseNumber,theAddin,theSum); System.out.printf("%4d %24.20f %24.20f %24.20f%n",i,theBaseNumber,theAddin,theSum); } // for(int i = 1; i <= 53; ++i) } // public void doDoubleTest3(); /* **************************************************************************** */ /** test double precision arithmetic for money **/ public void doTenCentsTest() { double tenCents,theSum; System.out.println(""); System.out.println("Test of double precision arithmetic for money"); theSum = 1.00; tenCents = 1.00/10.0; System.out.println("Ten Cents is " + tenCents); System.out.printf("Ten Cents is %e%n",tenCents); for(int i = 1; i <= 100; ++i) { theSum = theSum + tenCents; System.out.println("Sum after " + i + " additions is " + theSum); } // for(int i = 1; i <= 10000; ++i) } // public void doTenCentsTest() } // public class TestCode