College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 1 COE1361: Computing for Engineers Learning Objectives Understand how numbers are stored in a computer and what the implications are Lecture 1c Precision and accuracy Numbers on a computer Exercises Summary Not in our textbook … pay attention … create your own knowledge base
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 2 Lecture References Online Computer memory – – – Numeric representation – – tm – – ieeefloat.html – node4.html Standards – /
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 3 How Does a Computer Work? In Lecture 1b we looked at: The history of computers The basic hardware structure of a microprocessor How the hardware relates to the software Limitations in representation (precision/accuracy) Binary Representation IEEE Floating Point Relation to Matlab In Lecture 1c (this one), we will consider numeric representation:
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 4 Numbers: Precision and Accuracy Low precision: = 3.14 High precision: = Low accuracy: = High accuracy: = High accuracy & precision: = Good Accuracy Good Precision Good Precision Poor Accuracy Good Accuracy Poor Precision Poor Accuracy Poor Precision
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 5 Numbers Numbers we use are DECIMAL (or base 10): –Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 –123 = 1* * *10 0 But we can always use other bases: Octal (base 8): –Digits: 0, 1, 2, 3, 4, 5, 6, 7 –123 = 1* * *8 0 –123 8 = = Binary (base 2): –Digits: 0, 1 –1011 = 1* * * *2 0 – = = –123 8 = = Hexadecimal (base 16): –Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F –123 = 1* * *16 0 – = = – = =
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 6 Computer Memory Results of a computation, graphics, word documents, everything that a computer does must be stored somewhere. That “somewhere” is “memory”. Memory comes in a variety of types and speeds: Cache – in the CPU itself (fastest) RAM - external to the CPU (fast) Disk - physical media, external to the CPU, r/w CDROM - physical media, (slow) Tape - physical media, (slowest) Memory is measured in “bytes” (and kilobytes, megabytes, gigabytes, and terabytes.)
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 7 Inside the Bytes A byte is the smallest memory allocation available. A byte contains 8 bits so that: –Smallest: = 0 10 –Largest: = 1*2 7 +1*2 6 +1*2 5 +1*2 4 +1*2 3 +1*2 2 +1*2 1 +1*2 0 = (or ) Result: a single byte can be used to store an integer number ranging from 0 to 255 (256 different numbers) If negative numbers are included, one bit must be dedicated to the sign, leaving only 7 bits for the number –Smallest: 0 –Largest: or
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 8 Signed-Magnitude (OPTIONAL MATERIAL) Example of signed magnitude: => => - 5 Notice also that signed-magnitude gives plus and minus zero! – => + 0 – => - 0
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 9 Two’s Complement Used (OPTIONAL MATERIAL) If first bit is zero, the number is positive and there is no further interpretation needed: – => If first bit is one, the number is negative. Complement all bits and add one to get to the positive magnitude of the negative number Therefore original number = -1
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 10 Two’s Complement (OPTIONAL MATERIAL) Two’s Complement has no negative zero = - 1, = -128 Range: [ -128, 127 ]
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 11 Inside the Bytes (255) is not a very big number, so computers generally use multiple bytes to represent numbers: Here is some new vocabulary: –byte = 8 bits –short (precision) = 2 bytes –single (precision) = 4 bytes –double (precision) = 8 bytes –quad (precision) = 16 bytes –char = 2 bytes (used to be 1 byte) Note: –A word is the basic size of the CPU registers and for Pentium chips it is 4 bytes or 32 bits; it is 8 bytes for the new Itanium and some unix chipsets. It was 2 bytes for early Intel chips; some new game consoles use 16 byte words. Java and other languages use different names!
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 12 Inside the Bytes How are fractional numbers (called real or floating point numbers) stored in a computer? The IEEE 754 double format consists of three fields: –a 52-bit fraction, f (also called the mantissa) –an 11-bit biased exponent, e –and a 1-bit sign, s These fields are stored contiguously in 8 bytes (or 2 successively addressed 4-byte words):
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 13 Inside the Bytes Because only a finite number of bits are used for each part of the number, not all possible real numbers can be represented in a computer using IEEE Positive numbers smaller than (positive underflow, Matlab “realmin”) Positive numbers greater than ( ) x (positive overflow, Matlab “realmax”) Negative numbers less than -( ) x (negative overflow) Negative numbers greater than (negative underflow) RESULT: about 16 digits of precision in the range ±10 ±308 Zero (actually is a special combination of bits)
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 14 Inside the Bytes Others sources of error in computation: Errors in the input data - measurement errors, errors introduced by the conversion of decimal data to binary, roundoff errors. Roundoff errors during computation (as discussed) Truncation errors - using approximate calculation is inevitable when computing quantities involving limits and other infinite processes on a computer –Never try to compare two floating point numbers for equality because all 16+ digits would have to match perfectly…
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 15 Describing Error in Matlab Accuracy How close your answer is to the “actual” or “real” answer. Precision The smallest difference that can be represented on the computer ( help eps ) Recognize: MATLAB (and other programs that use IEEE doubles) give you “good” digits MATLAB will also store exact integer values using only the mantissa (for about digits total) Matlab commands: realmin, realmax, eps, bitmax (try with help )
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 16 Memory in MATLAB
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 17 What it Means… In the previous slide, we see: What is the size of the variable “i” What does “class” represent? How many bytes are used to store the value? Name Size Bytes Class i 1x1 8 double array s 1x43 86 char array t 1x double array
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 18 Back to MATLAB - what does all this mean? We must pay attention to the math! (on any computer!) Given a finite (limited) number of bits, almost all computations will result in numbers that can’t be represented exactly. Remember that the result of a floating-point computation must be truncated to fit back into it’s finite representation. Always check your results - design programs to allow for independent verification of computations!
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 19 SUMMARY Describe memory. List different kinds of memory. What is IEEE 754? Describe how MATLAB represents numbers. Draw a number line and identify ranges where computers will generate errors. Describe three potential sources of errors in computation. Describe precision. Describe accuracy. Describe how we can protect ourselves from computation error.
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 20 Summary Action Items Review the lecture How will you use this information in your current work? … future work at GT? Start Exploring Matlab! Increment your record of commands learned Come prepared to ask questions about MATLAB Lecture 1c Numbers on a computer Precision and accuracy