numeric representation in a computer

28
Fall 2006 AE6382 Design Computing 1 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

Upload: essien

Post on 05-Jan-2016

47 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 1

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

Page 2: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 2

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 PrecisionPoor Accuracy

Good AccuracyPoor Precision

Poor AccuracyPoor Precision

Page 3: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 3

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

Page 4: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 4

Computer Memory is Varied…

Page 5: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 5

Memory in MATLAB

Page 6: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 6

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

Page 7: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 7

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

Page 8: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 8

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

Page 9: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 9

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.

Page 10: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 10

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?

Page 11: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 11

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

Page 12: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 12

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)

Page 13: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 13

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…

Page 14: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 14

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)

Page 15: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 15

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!

Page 16: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 16

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.

Page 17: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 17

Numeric computation in MATLAB

Page 18: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 18

Simple Math and Evaluation

• Expressions are evaluated using standard algebraic hierarchy, with parenthesis overriding the normal convention. (help ops)

Operation Symbol Example

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!

Page 19: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 19

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

Page 20: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 20

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 “;”

Page 21: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 21

Practice: simple computations

• Try the following MATLAB code:– Example 1

X = 5 ^ 2;

Y = 2 * 2;

Z = X * Y;

Z

– Example 2Price = 19.95;

Tax = 0.07;

Units = 3;

Cost = (Units * Price) * (1.0 + Tax )

• What about the “;”? What is Y? What is Price?

Page 22: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 22

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!

Page 23: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 23

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

Page 24: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 24

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!

Page 25: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 25

Practice: Matlab Expressions

• Write MATLAB expressions for the following:

21

v

c

v

bfactor

12

12

xx

yyslope

321

1111

rrr

resist

asr

asrcenter

)(

sin)(1972.3822

33

Page 26: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 26

Summary

• Topics

• Memory• Numbers on a

computer• Precision and accuracy• Numeric operators• Precedence• Writing complicated

expressions.

Page 27: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 27

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/

Page 28: Numeric Representation in a Computer

Fall 2006AE6382 Design Computing 28

MATLAB commands used

• format rat• format short• format long• clear• who• whos• realmin• realmax• eps

• help arith• help slash• help ops• help format