Presentation is loading. Please wait.

Presentation is loading. Please wait.

College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 1 COE1361:

Similar presentations


Presentation on theme: "College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 1 COE1361:"— Presentation transcript:

1 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

2 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 –http://www-und.ida.liu.se/~annsa582/tutorial/ –http://www.crisinc.addr.com/memory.html –http://www.howstuffworks.com/computer-memory.htm Numeric representation –http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html –http://www.math.byu.edu/~schow/work/floating_point_system.h tm –http://www.cs.utah.edu/~zachary/isp/applets/FP/FP.html –http://www.research.microsoft.com/~hollasch/cgindex/coding/ ieeefloat.html –http://www.cs.unc.edu/~dm/UNC/COMP205/LECTURES/ERROR/lec23/ node4.html Standards –http://standards.ieee.org /

3 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:

4 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:  = 3.140101011 Low accuracy:  = 3.10212 High accuracy:  = 3.14159 High accuracy & precision:  = 3.141592653 Good Accuracy Good Precision Good Precision Poor Accuracy Good Accuracy Poor Precision Poor Accuracy Poor Precision

5 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 2 + 2*10 1 + 3*10 0 But we can always use other bases: Octal (base 8): –Digits: 0, 1, 2, 3, 4, 5, 6, 7 –123 = 1*8 2 + 2*8 1 + 3*8 0 –123 8 = 64+16+3 = 83 10 Binary (base 2): –Digits: 0, 1 –1011 = 1*2 3 + 0*2 2 + 1*2 1 + 1*2 0 –1011 2 = 8+0+2+1 = 11 10 –123 8 = 001 010 011 = 1010011 2 Hexadecimal (base 16): –Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F –123 = 1*16 2 + 2*16 1 + 3*16 0 –123 16 = 256 + 32 + 3 = 291 10 –123 16 = 0001 0010 0011 = 100100011 2

6 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.)

7 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 0 0 0 0 0 0 0 = 0 10 –Largest: 1 1 1 1 1 1 1 1 = 1*2 7 +1*2 6 +1*2 5 +1*2 4 +1*2 3 +1*2 2 +1*2 1 +1*2 0 = 255 10 (or 2 8 -1) 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: +127 10 or -128 10

8 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: 00000101 2 => + 5 10000101 2 => - 5 Notice also that signed-magnitude gives plus and minus zero! –00000000 2 => + 0 –10000000 2 => - 0

9 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: –01111111 2 => + 127 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. 11111111 2 00000000 + 1 00000001 2 Therefore original number = -1

10 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. 11111111 = - 1, 10000000 = -128 Range: [ -128, 127 ]

11 College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 11 Inside the Bytes 2 8 -1 (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!

12 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):

13 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 754 0 Positive numbers smaller than 2 -1022 (positive underflow, Matlab “realmin”) Positive numbers greater than (2-2 -52 ) x 2 1023 (positive overflow, Matlab “realmax”) Negative numbers less than -(2-2 -52 ) x 2 1023 (negative overflow) Negative numbers greater than -2 -1022 (negative underflow) RESULT: about 16 digits of precision in the range ±10 ±308 Zero (actually is a special combination of bits)

14 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…

15 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 15-16 “good” digits MATLAB will also store exact integer values using only the mantissa (for about 15-16 digits total) Matlab commands: realmin, realmax, eps, bitmax (try with help )

16 College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 16 Memory in MATLAB

17 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 1x200 1600 double array

18 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!

19 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.

20 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


Download ppt "College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 1 COE1361:"

Similar presentations


Ads by Google