Java page 6 ccsun5 testjava > cat -n TestPrecision.java 1 // TestPrecision.java -- CopyLeft by tsaiwn@csie.nctu.edu.tw 2 // to Test (1)precision, (2)computation rule for floating expression 3 // please compare to floatdbl.c in our C/C++ handouts 4 //Also test the format capability of the java.text.Format class 5 //It and it's subclasses provides data editing similar to printf() in C Lang. 6 import java.text.*; // Format, NumberFormat, DecimalFormat, ... 7 public class TestPrecision { 8 static final String f4dot2 = "0.00"; //Integer part at least one digit 9 private static String myformat(float x){ 10 DecimalFormat f = new DecimalFormat(" #######00.000####"); 11 return f.format(x); //at least like 99.999 12 } 13 private static String myformat(double x){ // rewrite for double 14 DecimalFormat f = new DecimalFormat("########0.0000000"); 15 return f.format(x); // the above means f.setMaximumFractionDigits(7); 16 } 17 private static String myfm2(double x){ 18 DecimalFormat f = new DecimalFormat(f4dot2); 19 f.setMinimumIntegerDigits(5); // at least 5 digits in Integer part 20 return f.format(x); 21 } 22 public static void main( String p[]) { 23 float x, xdelta; 24 double y; long i; 25 x = (float)1234567.2; 26 xdelta = 0.0001f; 27 System.out.println("Before loop, x="+ x); 28 System.out.println(" (double)x="+ (double)x); 29 System.out.println(" ===" + myformat(x) ); 30 System.out.println("xdelta="+ myformat(xdelta)); 31 for(i=1; i<= 8000; i++){ 32 x = x + xdelta; /******/ 33 } 34 System.out.println(" After loop, x="+ x); 35 System.out.println(" (double)x="+ (double)x); 36 System.out.println("\nDo it again with double y ..."); 37 y = 1234567.2; 38 xdelta = 0.0001F; 39 System.out.println("Before loop, y="+ y); 40 System.out.println(" ===" + myformat(y) ); 41 for(i=1; i<= 8000; i++){ 42 y = y + xdelta; /*** promotion is allowed, not coercion ***/ 43 } /*** x = y; is not allowed ! ***/ 44 System.out.println(" After loop, y="+ y); 45 System.out.println(" ===" + myformat(y) ); 46 System.out.println(" == myfm2() has format pattern"+ 47 " \"0.00 with modification through its method\"=="); 48 System.out.println(" which means %4.2f in C Language"); 49 System.out.println(" myfm2(123.564)=" + myfm2(123.564) ); 50 System.out.println(" myfm2(123.566)=" + myfm2(123.566) ); 51 System.out.println(" myfm2(123.565)=" + myfm2(123.565) ); 52 System.out.println(" 注意不是真的四捨五入, 這符合IEEE754的規定"); 53 System.out.println("3849==="+ new DecimalFormat("##000000").format(3849)); 54 } 55 } ccsun5 testjava > /usr/local/jdk/jdk1.2.2/bin/javac TestPrecision.java sjhuang has replaced srchao on pts/17 from openbsd. ccsun5 testjava > /usr/local/jdk/jdk1.2.2/bin/java TestPrecision Before loop, x=1234567.2 (double)x=1234567.25 === 1234567.250 xdelta= 00.0001 After loop, x=1234567.2 (double)x=1234567.25 Do it again with double y ... Before loop, y=1234567.2 ===1234567.2000000 After loop, y=1234568.0000005036 ===1234568.0000005 == myfm2() has format pattern "0.00 with modification through its method"== which means %4.2f in C Language myfm2(123.564)=00123.56 myfm2(123.566)=00123.57 myfm2(123.565)=00123.56 注意不是真的四捨五入, 這符合IEEE754的規定 3849===003849 ccsun5 testjava > exit