1 coms 161 introduction to computing title: numeric processing date: october 22, 2004 lecture...
TRANSCRIPT
1
COMS 161Introduction to Computing
Title: Numeric Processing
Date: October 22, 2004
Lecture Number: 24
2
Announcements
• Exam 2– Monday 10/25/2004
– Covers• LANs Chapter 4• The Internet Chapter 17• HTTP ands HTML Chapter 18
• Today’s Material – Chapter 6
3
Review
• Numeric Processing
• Integers– Magnitude representation– Sign-magnitude representation– Two’s complement numbers
4
Outline
• Numeric Processing– Real numbers
5
Magnitude Numbers
• Use straight decimal to binary conversion, where
N10 = an* 2n + an-1* 2n-1 + … a1* 21 + a0* 20
• The coefficients (an, an-1, … a1, a0) correspond to the binary representation
1110 = 1*23 + 0*22 + 1*21 + 1*20
1110 = 1 0 1 1
6
Magnitude Numbers
• Can represent 2n numbers– n is the number of bits
{ 0, 1, 2, …, 2n – 1 }• n = 0, 2n = 20 = 1, { 0 }• n = 1, 2n = 21 = 2, { 0, 1 }• n = 2, 2n = 22 = 4, { 0, 1, 2, 3 }• n = 3, 2n = 23 = 8, { 0, 1, 2, 3, 4, 5, 6, 7 }
• Main problem:– No representation for negative numbers
7
Sign-Magnitude Numbers
• Provides a representation for negative numbers– Most significant bit (MSB) is
• 0 for positive numbers• 1 for negative numbers
– One half of the numbers represent • Positive numbers
– The other half of the numbers represents• Negative numbers
8
Sign-Magnitude Numbers
• The positive numbers for n bits
{ 0, 1, 2, …, ((2n) / 2) – 1 }
{ 0, 1, 2, …, 2n-1 – 1 }• n = 0, 2n-1 - 1= 2-1 - 1= -1, { . }
– Not even a positive number!!!– Can not represent any numbers with 0 bits!!!
• n = 1, 2n-1 – 1 = 20 – 1 = 0, { 0 }– Can represent the sign bit with a single bit!!!
• n = 2, 2n-1 – 1 = 21 – 1 = 1, { 0, 1 }– Two bits, one sign bit one magnitude bit, (2 values)
9
Sign-Magnitude Numbers
– Positive numbers represented• n = 2, 2n-1 – 1 = 21 – 1 = 1, { 0, 1 }
• n = 3, 2n-1 – 1 = 22 – 1 = 3, { 0, 1, 2, 3 }
• n = 4, 2n-1 – 1 = 23 – 1 = 7, {0, 1, 2, 3, 4, 5, 6, 7}
0 0 0 1
0 0 0 0 0 1 0 1 0 0 1 1
0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1
0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1
0 1
0 1
0 1 2 3
2 3 4 5 6 7
0 0 0 1
0 0 0 0 0 1 0 1 0 0 1 1
0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1
0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1
10
Sign-Magnitude Numbers
• The negative numbers for n bits
{-((2n) / 2) + 1, -((2n) / 2) + 2, … -1, 0 }
{-2n-1 + 1, -2n-1 + 2, … -1, 0 }• n = 0, -2n-1 + 1 = -2-1 + 1 = 1
– Not even a negative number!!!
– Can not represent any numbers with 0 bits!!!
• n = 1, -2n-1 + 1 = -20 + 1 = 0, { 0 }– Can represent the sign bit with a single bit!!!
• n = 2, -2n-1 + 1 = -21 + 1 = -1, { -1, 0 }– Two bits, one sign bit one magnitude bit, (2 values)
11
Sign-Magnitude Numbers
– Negative numbers represented• n = 2, -2n-1 + 1 = -21 + 1 = -1, { -1, -0 }
• n = 3, -2n-1 + 1 = -22 + 1 = -3, { -3, -2, -1, -0 }
• n =4,-2n-1+1= -23+1 = -7, {-7,-6,-5,-4,-3,-2,-1,-0}
1 1 1 0
1 1 1 1 1 0 1 0 1 1 0 0
1 1 1 1 1 1 1 0 1 1 0 1 1 1 0 0
1 0 1 1 1 0 1 0 1 0 0 1 1 0 0 0
-1 -0
-3 -2 -1 -0
-7 -6 -5 -4 -3 -2 -1 -0
1 1 1 0
1 1 1 1 1 0 1 0 1 1 0 0
1 1 1 1 1 1 1 0 1 1 0 1 1 1 0 0
1 0 1 1 1 0 1 0 1 0 0 1 1 0 0 0
12
Sign-Magnitude
• Mathematical operations sometimes give an incorrect result
4 – 3 = 4 + -3 = 1
4 0100+(-3) +1011
1
1
1
1
1
-7
13
Two’s Complement Numbers
• Two steps– Invert bits of the magnitude representation
of the number
– Add one (1) to the result
-710 Magnitude number: -710Magnitude representation: 0111Bit inversion: 1000Add one: 1001
-710 in two’s complement is: 1001
14
Two’s Complement
• Mathematical operations give a correct result
4 – 3 = 4 + -3 = 1
4 0100+(-3) +1101
1
1
0
0
0
1
1 1
Ignore the carry-out
15
Two’s Complement
• Mathematical operations give a correct result
3 – 4 = 3 + -4 = -1
3 0011+(-4) +1100
1
1
1
1
1
1
16
Two’s Complement
• Mathematical operations give a correct result
-3 – 4 = -3 + -4 = -7
-3 1101+(-4) +1100
-7
1
0
0
1
-7
1 1
Ignore the carry-out
17
Two’s Complement Numbers
• Solves the two problems of sign-magnitude numbers– Two representations of zero problem
– Mathematical operations• Give the correct result• Result is in two’s complement representation
18
Real Numbers
• Scientific Notation– Number times 10 to a power
123456.0 = 123.456 * 103
• Normalized scientific notation– Zero to the left of the decimal point
123456.0 = 0.123456 * 106
• Advantageous to store real numbers in 32 bits (word size)
19
Real Numbers
22 21 20 2 -1 2 -2 2 -3 2 -4
4 2 1 0.5 0.25 0.125 0.0625
0.1012 = 0 * 20 + 1 * 2-1 + 0 * 2-2 + 1 * 2-3
0.1012 = 0.5 + 0.125 = 0.62510
4 2 1 1/2 1/4 1/8 1/16
20
Real Numbers
1210 = 0 * 24 + 1 * 23
24 23 22 21 20
16 8 4 2 1
1210 – 810 = 410
410 = 1 * 22
410 – 410 = 010
0 1 1 0 02
Decimal to binary conversion algorithm
21
Real Numbers
20 = 12 -1 = 0.52 -2 = 0.252 -3 = 0.1252 -4 = 0.06252 -5 = 0.031252 -6 = 0.0156252 -7 = 0.00781252 -8 = 0.00390625
0.87510 – 0.5 = 0.37510
0.87510 = 1 * 2 -1
0.37510 = 1 * 2 -2
0.37510 – 0.25 = 0.12510
0.12510 = 1 * 2 -3
0.12510 – 0.125 = 010
0.2
1 1 1
0.87510 = 0.?????2
22
Real (Decimal) Number Storage
• Real numbers are stored in floating point representation– A sign
– An exponent
– A mantissa (normalized decimal fraction)• No digits to the left of the decimal• First digit to the right of the decimal is nonzero
23
Real (Decimal) Number Storage
• Floating point number representation– 32-bit
s eeeeeeee fffffff ffffffffffffffff
– s: sign bit
– e: exponent bits [-126 … 127]
– f: fractional part [23 bits + 1 implied bit]
24
Real (Decimal) Number Storage
• Numbers have limited precision
– Most real numbers have an infinite decimal expansion
25
Real Number StorageLimited Range and Precision
• There are three categories of numbers left out when floating point representation is used– Numbers out of range because their absolute
value is too large (similar to integer overflow)– Numbers out of range because their absolute
value is too small (numbers too near zero to be stored given the precision available
– Numbers whose binary representations require either an infinite number of binary digits or more binary digits than the bits available
26
Real Number StorageLimited Range and Precision Illustrated
With one bit to the right of the decimal point, only the real number 0.5 can be represented.
27
Real Number StorageLimited Range and Precision Illustrated
real numbers that can be represented with two bits
0.25, 0.5, 0.75
real numbers that can be represented with three bits
0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875
The holes correspond to all the unrepresented numbers: 0.126, 0.255, 0.3, …
28
Limited Range and Precision Some Consequences
• Limited range will invalidate certain calculations– If integers are involved, this can often be
avoided by switching to real numbers
– For real number calculations, this problem arises infrequently and in those cases can sometimes be handled by special methods
– It is not a common occurrence in non-scientific work
29
Limited Range and Precision Some Consequences
• Limited precision for real numbers is very pervasive– Assume that most decimal calculations
will, in fact, be in error!
– Evaluate and use computer calculations with this in mind
30
Social ThemesRisks in Numerical Computing
• Almost all computer calculations involve roundoff error (limited precision error)
• If not monitored and planned for carefully, such errors can lead to unexpected and catastrophic results– Arianne 5 Rocket Failure
– Patriot Missile Failure during Gulf War
31
Software for Numerical Work
• Software Libraries
• Spreadsheets
• Mathematical Software– symbolic manipulation
– data analysis
– data visualization
32
CSC 101Overview of Computer Science
Lecture 1503/02/2004
Numeric Processing
33
Integer Storage
• Integers are typically 32 bits (word size)
• Little-endian: intel– Most significant byte on the right
• Big-endian: SUN, SGI– Most significant byte on the left
LSB MSB
MSB LSB
34
Integer Storage
• Problem occurs when a binary file is transferred between different endian machines
Pow-Wow
35
Integer Storage
• Range of integer numbers– 32 bits (4 bytes) per integer
-2,147,483,648 … 2,147,483,647
integer overflow error – Trying to represent an integer that is larger than
the most positive allowable integer or more negative than most negative integer
Show Overflow
36
Binary to Decimal Conversion
• Recall positional notation– Position of the digit determines its values
110110112 = 1*27 + 1*26 + 0*25 + 1*24 +
1*23 + 0*22 + 1*21 + 1*20
= 1*128 + 1*64 + 1*16 + 1*8 +
1*2 + 1*1
= 128 + 64 + 16 + 8 + 2 + 1
= 21910
37
Binary to Decimal Conversion
• Algorithm:
Binary digits: 1 0 0 1 1…
Base 2 powers: 2n 2n-1 22 21 20…
Multiply columns: 2n 21 20…
Add values: 2n + … + 21 + 20 = number10
38
Decimal to Binary Conversion
• Algorithm:– Subtract the largest power of 2 that is less than
or the same as the decimal number from the decimal number
– Place a 1 in the 24 position
Change 2710 to its binary representationLargest power of 2 less than 2710 is 4 since
24 ≤ 16 < 27 < 32 = 25
27 – 16 = 11
39
Decimal to Binary Conversion
• Algorithm:
– Repeat until the decimal value is 0
22 21 2024 23
* * *1 *
23 = 8 ≤ 11 < 16 = 24
11 - 8 = 3
22 21 2024 23
* * *1 1
21 = 2 ≤ 3 < 4 = 22
3 – 2 = 1
22 21 2024 23
* 1 *1 1
20 ≤ 1
1 – 1 = 0
22 21 2024 23
* 1 11 1
40
Decimal to Binary Conversion
• Algorithm:– Put zeros where there are *’s
– Can add leading zeros if you like
22 21 2024 23
0 1 121 12710 =
41
Real Numbers
• Scientific Notation– Number times 10 to a power
123456.0 = 123.456 * 103
• Normalized scientific notation
– Single digit to the left of the decimal point
123456.0 = 1.23456 * 105
• Advantageous to store real numbers in 32 bits (word size)
42
Real (Decimal) Number Storage
• Real numbers are stored in floating point representation
• IEEE Standard 754– Allows using data on different machines– A sign– An exponent– A mantissa also called a significand (normalized
decimal fraction)• Single digit to the left of the decimal point
43
Real (Decimal) Number Storage
• IEEE standard 754– floating point number representation– 32-bit
s eeeeeeee fffffff ffffffffffffffff
– s: (1) sign bit• 0 means positive, 1 means negative
44
Real (Decimal) Number Storage
s eeeeeeee fffffff ffffffffffffffff – e: (8) exponent bits [-126 … 127]
• A bias of 127 is added to the exponent• Exponent of 0 is stored as 127, stored exponent of
200 means actual exponent is (200 – 127) = 73• Stored exponent of all zeros and ones are reserved
for special numbers
– f: (24) fractional part [23 bits + 1 implied bit]• Since number to the left of the decimal point is not
zero, its binary representation will have a leading one• Saves a bit, a one is implied and does not need to be
explicitly stored
45
Real (Decimal) Number Storage
• 32-bit floating point number has less precision that a 32-bit integer number
• Floating point number has 24-bits resolution
seeeeeee e f f f f f f f f f f f f f f f f f f f f f f f
Byte 0 1 2 3
46
Real (Decimal) Number Storage
• Can represent– A large range of floating point numbers
– Numbers to greater precision
• By adding more bytes
seeeeeee e f f f f f f f f f f f f f f f f f f f f f f f
Byte 0 1 2 3
47
Real (Decimal) Number Storage
• Double precision floating point numbers
– s: (1) sign bit
– e: (11) exponent bits [-1023 … 1024]
– f: (53) fractional part [52 bits + 1 implied bit]
seeeeeee eee f f f f f f f f f f f f f f f f f f f f
Byte 0 1 2 3
f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f
Byte 4 5 6 7
48
Real (Decimal) Number Storage
• Numbers have limited precision
Compute 1
49
Real Number StorageLimited Range and Precision
• There are three categories of numbers left out when floating point representation is used– Numbers out of range because their absolute
value is too large (similar to integer overflow)– Numbers out of range because their absolute
value is too small (numbers too near zero to be stored given the precision available
– Numbers whose binary representations require either an infinite number of binary digits or more binary digits than the bits available
50
Real Number StorageLimited Range and Precision Illustrated
51
Limited Range and Precision Some Consequences
• Limited range will invalidate certain calculations– If integers are involved, this can often be
avoided by switching to real numbers
– For real number calculations, this problem arises infrequently and in those cases can sometimes be handled by special methods
– It is not a common occurrence in non-scientific work
52
Limited Range and Precision Some Consequences
• Limited precision for real numbers is very pervasive– Assume that most decimal calculations
will, in fact, be in error!
– Evaluate and use computer calculations with this in mind
53
Social ThemesRisks in Numerical Computing
• Almost all computer calculations involve roundoff error (limited precision error)
• If not monitored and planned for carefully, such errors can lead to unexpected and catastrophic results– Arianne 5 Rocket Failure
– Patriot Missile Failure during Gulf War
54
Software for Numerical Work
• Software Libraries
• Spreadsheets
• Mathematical Software– symbolic manipulation
– data analysis
– data visualization
55
CSC 101Overview of Computer Science
Lecture 1603/02/2004
Numeric Processing
56
Real (Decimal) Number Storage
• Real numbers are stored in floating point representation
• IEEE Standard 754– Allows using data on different machines– A sign– An exponent– A mantissa also called a significand (normalized
decimal fraction)• Single digit to the left of the decimal point
57
Real (Decimal) Number Storage
• IEEE standard 754– floating point number representation– 32-bit
s eeeeeeee fffffff ffffffffffffffff
– s: (1) sign bit• 0 means positive, 1 means negative
58
Real (Decimal) Number Storage
s eeeeeeee fffffff ffffffffffffffff – e: (8) exponent bits [-126 … 127]
• A bias of 127 is added to the exponent• Exponent of 0 is stored as 127, stored exponent of
200 means actual exponent is (200 – 127) = 73• Stored exponent of all zeros and ones are reserved
for special numbers
– f: (24) fractional part [23 bits + 1 implied bit]• Since number to the left of the decimal point is not
zero, its binary representation will have a leading one• Saves a bit, a one is implied and does not need to be
explicitly stored
59
Real (Decimal) Number Storage
• 32-bit floating point number has less precision that a 32-bit integer number
• Floating point number has 24-bits resolution
seeeeeee e f f f f f f f f f f f f f f f f f f f f f f f
Byte 0 1 2 3
60
Real (Decimal) Number Storage
• Can represent– A large range of floating point numbers
– Numbers to greater precision
• By adding more bytes
seeeeeee e f f f f f f f f f f f f f f f f f f f f f f f
Byte 0 1 2 3
61
Real (Decimal) Number Storage
• Double precision floating point numbers
– s: (1) sign bit
– e: (11) exponent bits [-1023 … 1024]
– f: (53) fractional part [52 bits + 1 implied bit]
seeeeeee eee f f f f f f f f f f f f f f f f f f f f
Byte 0 1 2 3
f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f
Byte 4 5 6 7
62
Real (Decimal) Number Storage
• Numbers have limited precision
Compute 1
63
Real Number StorageLimited Range and Precision
• There are three categories of numbers left out when floating point representation is used– Numbers out of range because their absolute
value is too large (similar to integer overflow)– Numbers out of range because their absolute
value is too small (numbers too near zero to be stored given the precision available
– Numbers whose binary representations require either an infinite number of binary digits or more binary digits than the bits available
64
Real Number StorageLimited Range and Precision Illustrated
65
Limited Range and Precision Some Consequences
• Limited range will invalidate certain calculations– If integers are involved, this can often be
avoided by switching to real numbers
– For real number calculations, this problem arises infrequently and in those cases can sometimes be handled by special methods
– It is not a common occurrence in non-scientific work
66
Limited Range and Precision Some Consequences
• Limited precision for real numbers is very pervasive– Assume that most decimal calculations
will, in fact, be in error!
– Evaluate and use computer calculations with this in mind
67
Social ThemesRisks in Numerical Computing
• Almost all computer calculations involve roundoff error (limited precision error)
• If not monitored and planned for carefully, such errors can lead to unexpected and catastrophic results– Arianne 5 Rocket Failure
– Patriot Missile Failure during Gulf War
68
Software for Numerical Work
• Software Libraries
• Spreadsheets
• Mathematical Software– symbolic manipulation
– data analysis
– data visualization