Docsity

Floating-Point Datatypes in Java: Understanding Floats and Doubles - Prof. Marc L. Smith, Study notes of Computer Science

Toggle sidebar
bg1
Chapter 10
Floating-Point Datatypes
CMPU-101: Problem-Solving and Abstraction
Marc Smith
Vassar College
Spring 2007
2
Floating-Point Numbers
decimal points 4.73
3.14159
0.0825
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_754
This material will be covered thoroughly in:
CMPU-224 (Computer Organization)
5
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 + '.');
6
Prog1002
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);
pf3
pf4

Partial preview of the text

Download Floating-Point Datatypes in Java: Understanding Floats and Doubles - Prof. Marc L. Smith and more Study notes Computer Science in PDF only on Docsity!

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

  1. 1 f 1. 7
  2. 2 f 1. 69
  3. 3 f 1. 690
  4. 4 f 1. 6900
  5. 5 f 1. 69000
  6. 6 f 1. 690000** 8 String.format("%6.3f",1.3*1.3) " " 1.690" 9 Arithmetic
      • / double operator double " double int operator int " int Mixed-Mode Arithmetic double operator int " double int operator double " double 10 Some Examples 2 / 4 * 5. 0 * 5.

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,โ€ฆ )