310 likes | 534 Views
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. Numbers: precision and accuracy.
E N D
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 AE6382 Design Computing
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 AE6382 Design Computing
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.) AE6382 Design Computing
Computer Memory is Varied… AE6382 Design Computing
Memory in MATLAB AE6382 Design Computing
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 AE6382 Design Computing
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*102 + 2*101 + 3*100 • But we can always use other bases: • Octal (base 8): • Digits: 0, 1, 2, 3, 4, 5, 6, 7 • 123 = 1*82 + 2*81 + 3*80 • 1238 = 64+16+3 = 8310 • Binary (base 2): • Digits: 0, 1 • 1011 = 1*23 + 0*22 + 1*21 + 1*20 • 10112 = 8+0+2+1 = 1110 • 1238 = 001 010 011 = 10100112 • Hexidecimal (base 16): • Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F • 123 = 1*162 + 2*161 + 3*160 • 12316 = 256 + 32 + 3 = 29110 • 12316 = 0001 0010 0011 = 1001000112 AE6382 Design Computing
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 = 010 • Largest: 1 1 1 1 1 1 1 1 = 1*27+1*26+1*25+1*24+1*23+1*22+1*21+1*20 = 25510 (or 28-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: +12710 or -12810 AE6382 Design Computing
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. AE6382 Design Computing
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? AE6382 Design Computing
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): AE6382 Design Computing
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 21023(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 21023(positive overflow) • Zero (actually is a special combination of bits) AE6382 Design Computing
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… AE6382 Design Computing
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) AE6382 Design Computing
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! AE6382 Design Computing
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. AE6382 Design Computing
Numeric computation in MATLAB AE6382 Design Computing
Simple Math and Evaluation • Expressions are evaluated using standard algebraic hierarchy, with parenthesis overriding the normal convention. (help ops) PRECEDENCE - the order expressions are evaluated! AE6382 Design Computing
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 NOTEUsing spaces before and after operators is a matter of style and readability AE6382 Design Computing
Variables and Names • A variable is a placeholder in memory • Variables containvalues • 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 “;” AE6382 Design Computing
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? AE6382 Design Computing
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! AE6382 Design Computing
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 AE6382 Design Computing
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! AE6382 Design Computing
Practice: Matlab Expressions • Write MATLAB expressions for the following: AE6382 Design Computing
Summary • Topics • Memory • Numbers on a computer • Precision and accuracy • Numeric operators • Precedence • Writing complicated expressions. AE6382 Design Computing
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/ieeefloat.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/ AE6382 Design Computing
format rat format short format long clear who whos realmin realmax eps help arith help slash help ops help format MATLAB commands used AE6382 Design Computing