Post on 12-Jan-2016
1
College of EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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 EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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://www.cs.unc.edu/~dm/UNC/COMP205/LECTURES/ERROR/lec23/
node4.html
Standards– http://standards.ieee.org/
3
College of EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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 EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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
5
College of EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
Numbers
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
Hexadecimal (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
6
College of EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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 EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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
8
College of EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
Signed-Magnitude (OPTIONAL MATERIAL)
Example of signed magnitude:
000001012 => + 5
100001012 => - 5
Notice also that signed-magnitude gives plus and minus zero!
– 000000002 => + 0
– 100000002 => - 0
9
College of EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
Two’s Complement Used (OPTIONAL MATERIAL)
If first bit is zero, the number is positive and there is no further interpretation needed:
– 011111112 => + 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.
111111112
00000000
+ 1 000000012
Therefore original number = -1
10
College of EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
Two’s Complement (OPTIONAL MATERIAL)
• Two’s Complement has no negative zero.
• 11111111 = - 1,
• 10000000 = -128
• Range: [ -128, 127 ]
11
College of EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
Inside the Bytes
28-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 EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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 EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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 21023 (positive overflow, Matlab “realmax”)
Negative numbers less than -(2-2-52) x 21023 (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 EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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 EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
Describing Error in Matlab
AccuracyHow close your answer is to the “actual” or “real” answer.
PrecisionThe 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” digitsMATLAB 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 EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
Memory in MATLAB
17
College of EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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 EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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 EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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 EngineeringCollege of EngineeringR
ep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Rep
resen
ting
Nu
mb
ers
in a
Com
pu
ter
Secti
on
B,
Sp
rin
g
Secti
on
B,
Sp
rin
g
20
03
20
03
COE1361: Computing for COE1361: Computing for EngineersEngineers
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