School of Computer Science, University of Birmingham. Java Lecture notes. M. D. Ryan. September 2000. Bits and bytes Week 2: data representation • Data is stored in the computer as bits. (20 cr only) – Bit stands for binary digit ; it has the value 0 or 1. • Data representation. • Bits by themselves are not much use, so we define a byte to be 8 bits. – A byte can store numbers from 0 to 255 (in binary, 00000000 to 11111111). byte arithmetic Interpretations of a byte -1 0 1 2 3 4 5 … -2 -3 -4 the byte unsigned signed -5 0000 0000 0 0 0000 0001 1 1 . . . 0000 0010 2 . . . . . . 0111 1110 126 126 0111 1111 127 127 128 -128 1000 0000 1000 0001 129 -127 . . . . . . . . . 254 -2 1111 1110 1111 1111 255 -1 126 -126 -127 1 - 2 1 7 2 8 Bounding errors Some numeric types type values size range • When Java works with integer types byte integer -128 to 127 (byte, short, int, long) short integer -32768 to 32767 it uses a signed representation . int integer -2147483648 to 2147483647 • This means that, if positive numbers are long integer incremented beyond their maximum value, -9223372036854774808 to 9223372036854774808 ± 3.4… e38 to ± 1.4…e-45 float they become negative numbers. “real” 9 decimal place precision ± 1.7…e308 to ± 4.9…e-324 double “real” 18 decimal place precision f 1
public class Bounds Character interpretation of a byte { public static void main (String[] args) the byte character { byte b=127; 0000 0000 0 <nul> 0101 1010 Z 90 prints -128 System.out.println(b+1); 0000 0001 1 ctrl-A . . . int i=2147483647; 0000 0010 2 ctrl-B 0110 0001 97 a System.out.println(i+10); prints - 2147483639 . . . . . . b . . . 0110 0010 98 float f = (float)3.4e38; 0010 0000 32 <space> . . . prints Infinity System.out.println(f*10); 0010 1011 43 + 0111 1010 122 z double d = 5e-324; 0011 0000 0 { 48 0111 1011 123 System.out.println(d/10); prints 0 0011 0001 49 1 0111 1111 127 <del> } . . . . . . . . . } 0100 0001 A 65 0100 0010 66 B Floating point numbers Floating point number representation • You’ve probably noticed that scientific calculator • Internally, Java stores all floating numbers as a mantissa stores large and small numbers in two parts: the and an exponent of 2. mantissa and the exponent of 10 : • For example, the number 0.2 is stored as − × × 23 3 5 . 28573 10 1 . 1001100110 ... 2 • float s have 23 bits for the mantissa and 8 for the exponent exponent; double s have 52 (mantissa) and 11 mantissa (exponent). • Note that (as in this example) numbers with finite • In Java, this is written 5.28573E23. decimal representations may have infinite binary represenations. Round-off errors Why does it print 434? • Round-off errors are a fact of life with floating • Because, in binary, point numbers. E.g., try (1/3)*3 by hand, or by your calculator. 100 * 4.35 • We also get effects like this in Java. Try = 1100100 * 100.010110011001100110… = 110110010.1111111111111… double f = 4.35; • and we truncate this by casting it to an int. int n = (int)(100*f); • Moral of story: use Math.round: System.out.println(n); // prints 434! double f = 4.35; int n = (int)Math.round(100*f); 2
Comparing floating point numbers Compute an infinite series 1 1 1 1 + + + + + 1 • It’s a bad idea to write � 2 4 8 16 if (f1 == f2) ... because even if f1 and f2 are meant to be the • We cannot compute it exactly, because same, they may evaluate to different numbers. there are infinitely many terms • Therefore, write • However, they make a negligible if (Math.abs(f1-f2)<0.001) ... contribution after a while for a suitably chosen precision 0.001. • Keep adding terms until they are less than 10E-6. public class Sum { public static void main (String[] args) { double sum = 0; double increment = 1.0; while (increment > 1E-6) { sum += increment; increment /= 2; System.out.println(sum); } } } 3
Recommend
More recommend