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