Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2006AE6382 Design Computing1 Numeric Representation in a Computer Learning Objectives Understand how numbers are stored in a computer and how the.

Similar presentations


Presentation on theme: "Fall 2006AE6382 Design Computing1 Numeric Representation in a Computer Learning Objectives Understand how numbers are stored in a computer and how the."— Presentation transcript:

1 Fall 2006AE6382 Design Computing1 Numeric Representation in a Computer Learning Objectives Understand how numbers are stored in a computer and how the computer operates on them. Topics Numbers on a computer Precision and accuracy Numeric operators Precedence Exercises Summary

2 Fall 2006AE6382 Design Computing2 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

3 Fall 2006AE6382 Design Computing3 Computer Memory Numbers and the results of numeric computations (along with other data such as text, graphics, documents, etc) must be stored somewhere in a computer. 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.)

4 Fall 2006AE6382 Design Computing4 Computer Memory is Varied…

5 Fall 2006AE6382 Design Computing5 Memory in MATLAB

6 Fall 2006AE6382 Design Computing6 Inside the Bytes 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 k 1x1 8 double array s 1x12 24 char array x 1x200 1600 double array Grand total is 213 elements using 1632 bytes

7 Fall 2006AE6382 Design Computing7 Numbers and their Bases 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 Hexidecimal (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

8 Fall 2006AE6382 Design Computing8 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

9 Fall 2006AE6382 Design Computing9 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 vocabulary: –byte = 8 bits –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 game consoles use 16 byte words.

10 Fall 2006AE6382 Design Computing10 Inside the Bytes: Exercise Let’s investigate how MATLAB stores numbers: Try the following MATLAB commands: format short 2^8 2^8-1 %(is the answer correct?) 2^64 2^64-1 % (is the answer correct?) Question: what is the largest value of the exponent so that the answer above is correct?

11 Fall 2006AE6382 Design Computing11 Inside the Bytes How are fractional (floating point) numbers stored in a computer? The IEEE 754 double format consists of three fields: –a 52-bit fraction, f –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):

12 Fall 2006AE6382 Design Computing12 Inside the Bytes Because only a finite number of bits can be used for each number, not all possible numbers can be represented: Negative numbers less than -(2-2 -52 ) x 2 1023 (negative overflow) Negative numbers greater than -2 -1022 (negative underflow) Positive numbers less than 2 -1022 (positive underflow) Positive numbers greater than (2-2 -52 ) x 2 1023 (positive overflow) Zero (actually is a special combination of bits)

13 Fall 2006AE6382 Design Computing13 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…

14 Fall 2006AE6382 Design Computing14 Describing error Precision The smallest difference that can be represented on the computer (help eps) Accuracy How close your answer is to the “actual” or “real” answer. Recognize: MATLAB (and other programs that use IEEE doubles) give you 15-16 “good” digits MATLAB COMMANDS realmin, realmax, eps (try with help )

15 Fall 2006AE6382 Design Computing15 Inside the bytes 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, Remember that the result of a floating-point computation must be ROUNDED to fit back into it’s finite representation Always check your results - design programs to allow for independent verification of computations!

16 Fall 2006AE6382 Design Computing16 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.

17 Fall 2006AE6382 Design Computing17 Numeric computation in MATLAB

18 Fall 2006AE6382 Design Computing18 Simple Math and Evaluation Expressions are evaluated using standard algebraic hierarchy, with parenthesis overriding the normal convention. ( help ops ) OperationSymbolExample Exponentiation^3 ^ 2 Multiplication*5 * 3 Division/ or \5 / 4 or 4 \ 5 Addition+6+2 Subtraction-5 – 3 PRECEDENCE - the order expressions are evaluated!

19 Fall 2006AE6382 Design Computing19 Practice: Simple Calculations What are the results of the following MATLAB expressions? –5 ^ 2 + 3 / 2 ^ 3 – 25 –(5-3)^2 –(5-3)*(2-1)\8 –2*pi –sin(pi/2) Try entering the values above in MATLAB Try entering help ops NOTE Using spaces before and after operators is a matter of style and readability

20 Fall 2006AE6382 Design Computing20 Variables and Names A variable is a placeholder in memory Variables contain values Variable names: –Are case sensitive: Cost, cost, COST are different –May contain up to 31 characters (more are ignored) –Must start with a letter, –May contain numbers and letters –May NOT contain punctuation except “_” How do I view the contents of a variable? –Just type the variable name without a following “;”

21 Fall 2006AE6382 Design Computing21 Practice: simple computations Try the following MATLAB code: –Example 1 X = 5 ^ 2; Y = 2 * 2; Z = X * Y; Z –Example 2 Price = 19.95; Tax = 0.07; Units = 3; Cost = (Units * Price) * (1.0 + Tax ) What about the “;”? What is Y? What is Price?

22 Fall 2006AE6382 Design Computing22 Where are Variables Stored? Variables are stored in a “workspace” Workspace commands: –who – display variables –whos – display variables and sizes –clear – removes variables from the work space –help clear – display help on command “clear” –clc - clears the command window (screen) –home - move cursor to top-left of command window Workspace is shown in the upper left MATLAB pane –Double-click on any variable to view it’s contents!

23 Fall 2006AE6382 Design Computing23 How are numbers displayed? Display is different from storage. MATLAB computes with 15-16 significant digits (IEEE double), but often shows less! The format command controls how values are displayed: –format short; format long –format short e; format long e; –format short g; format long g –format rat; format short –format compact; format loose Try using help format

24 Fall 2006AE6382 Design Computing24 MATLAB Built-in Functions MATLAB offers a wealth of built-in math functions that can be quite helpful for many computational problems Elementary MATLAB functions ( help elfun ) –Trigonometric functions –Exponential functions –Complex functions –Rounding and remainder functions Specialized MATLAB functions ( help specfun ) –Specialized math functions –Number theoretic functions –Coordinate transformations WE can write of own functions, also!

25 Fall 2006AE6382 Design Computing25 Practice: Matlab Expressions Write MATLAB expressions for the following:

26 Fall 2006AE6382 Design Computing26 Summary Topics Memory Numbers on a computer Precision and accuracy Numeric operators Precedence Writing complicated expressions.

27 Fall 2006AE6382 Design Computing27 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.htm http://www.cs.utah.edu/~zachary/isp/applets/FP/FP.html http://www.research.microsoft.com/~hollasch/cgindex/coding/ieeeflo at.html http://docs.sun.com/htmlcoll/coll.648.2/iso-8859- 1/NUMCOMPGD/ncg_goldberg.html http://www.cs.unc.edu/~dm/UNC/COMP205/LECTURES/ERROR/lec23/ node4.html Standards http://standards.ieee.org/

28 Fall 2006AE6382 Design Computing28 MATLAB commands used format rat format short format long clear who whos realmin realmax eps help arith help slash help ops help format


Download ppt "Fall 2006AE6382 Design Computing1 Numeric Representation in a Computer Learning Objectives Understand how numbers are stored in a computer and how the."

Similar presentations


Ads by Google