


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Chapter 10 Floating-Point Datatypes CMPU-101: Problem-Solving and Abstraction Marc Smith Vassar College Spring 2007 2 Floating-Point Numbers decimal points 4.
scientific notation 6 .023! 1023 9 .1! 10 -^31 3 Floating-Point Datatypes type bits from to float (^32) - 3. 4! 1038 3. 4! 1038 double (^64) - 1. 8! 10308 1. 8! 10308 Java assumes that floating-point literals are doubles. doubles have more range and precision than floats. We'll use only doubles. 4 Floating-Point Datatypes IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754) For the curious: http://en.wikipedia.org/wiki/IEEE_ This material will be covered thoroughly in: CMPU-224 (Computer Organization) Prog1001 โ Read a double & Print Its Square System.out.print("Enter a double: "); double x = kb.nextDouble(); System.out.println("The square of " + x + " is " + x*x + '.'); Prog 1002 double x = 1.3 * 1 .3; System.out.println(x + " without a format"); System.out.printf("%f in format %%f\n",x); System.out.printf("%6.0f in format %%6.0f\n",x); System.out.printf("%6.1f in format %%6.1f\n",x); System.out.printf("%6.2f in format %%6.2f\n",x); System.out.printf("%6.3f in format %%6.3f\n",x); System.out.printf("%6.4f in format %%6.4f\n",x); System.out.printf("%6.5f in format %%6.5f\n",x); System.out.printf("%6.6f in format %%6.6f\n",x);
7 Output control string prints as no format^1.^6900000000000002 **6. 0 f 2
2.0 / 3 * 5 0.666667 * 5
Shortcuts double x = 7.5; x += 3.3; x++; Methods from Class Math Math.abs(double) Math.sqrt(double) Math.sin(double) Math.pow(double,double)
19 In-Class Exercise add public boolean isBigger(Circle) 20 public void grow() { radius *= 1.1; } public void shrink() { radius *= 0.9; } Converting Strings to doubles Double.parseDouble(String) Converting doubles to Strings Rough: double x = โฆ String s = x + ""; For greater control: String.format( controlString,value,โฆ )