Download 10/22/04

Document related concepts

Ethnomathematics wikipedia , lookup

Foundations of mathematics wikipedia , lookup

History of logarithms wikipedia , lookup

Georg Cantor's first set theory article wikipedia , lookup

Infinity wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Infinitesimal wikipedia , lookup

Hyperreal number wikipedia , lookup

Location arithmetic wikipedia , lookup

Large numbers wikipedia , lookup

Approximations of π wikipedia , lookup

Real number wikipedia , lookup

Arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
COMS 161
Introduction to Computing
Title: Numeric Processing
Date: October 22, 2004
Lecture Number: 24
1
Announcements
• Exam 2
– Monday 10/25/2004
– Covers
• LANs
• The Internet
• HTTP ands HTML
Chapter 4
Chapter 17
Chapter 18
• Today’s Material
– Chapter 6
2
Review
• Numeric Processing
• Integers
– Magnitude representation
– Sign-magnitude representation
– Two’s complement numbers
3
Outline
• Numeric Processing
– Real numbers
4
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
5
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
6
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
7
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)
8
Sign-Magnitude Numbers
– Positive numbers represented
• n = 2, 2n-1 – 1 = 21 – 1 = 1, { 0,
0 1}
0 0
0 1
0 1,
1 2,
2 3}
• n = 3, 2n-1 – 1 = 22 – 1 = 3, { 0,
0 0 0
0 0 1
0 1 0
0 1 1
0 1,
1 2,
2 3
4 5,
5 6,
6 7
• n = 4, 2n-1 – 1 = 23 – 1 = 7, {0,
3, 4,
7}
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
9
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)
10
Sign-Magnitude Numbers
– Negative numbers represented
-0 }
-1 -0
• n = 2, -2n-1 + 1 = -21 + 1 = -1, { -1,
1 1
1 0
• n = 3, -2n-1 + 1 = -22 + 1 = -3, { -3,
-3 -2,
-2 -1,
-1 -0
-0 }
1 1 1
1 1 0
1 0 1
1 0 0
• n =4,-2n-1+1= -23+1 = -7, {-7,-6,-5,-4,-3,-2,-1,-0}
-7 -6 -5 -4 -3 -2 -1 -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
11
Sign-Magnitude
• Mathematical operations sometimes give
an incorrect result
4 – 3 = 4 + -3 = 1
4
+(-3)
1
0100
+1011
1 111
-7
12
Two’s Complement Numbers
• Two steps
– Invert bits of the magnitude representation
of the number
– Add one (1) to the result
-710 Magnitude number: -710
Magnitude representation: 0111
Bit inversion:
1000
Add one:
1001
-710 in two’s complement is: 1001
13
Two’s Complement
• Mathematical operations give a correct
result
4 – 3 = 4 + -3 = 1
1
4
0100
+(-3) +1101
1 1 0 001
1
Ignore the carry-out
14
Two’s Complement
• Mathematical operations give a correct
result
3 – 4 = 3 + -4 = -1
3
+(-4)
1
0011
+1100
1 111
1
15
Two’s Complement
• Mathematical operations give a correct
result
-3 – 4 = -3 + -4 = -7
1
-3
1101
+(-4) +1100
-7 1 1 001
-7
Ignore the carry-out
16
Two’s Complement Numbers
• Solves the two problems of signmagnitude numbers
– Two representations of zero problem
– Mathematical operations
• Give the correct result
• Result is in two’s complement representation
17
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)
18
Real Numbers
22
21
20
2 -1
4
2
1
1/2
4
2
1
0.5
2 -2
1/4
2 -3
2 -4
1/8
1/16
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
19
Real Numbers
Decimal to binary
conversion
algorithm
24
16
23
8
22
4
21
2
20
1
1210 = 0 * 24 + 1 * 23
1210 – 810 = 410
410 = 1 *
22
0
1
1
0
0
2
410 – 410 = 010
20
Real Numbers
20 = 1
2 -1 = 0.5
2 -2 = 0.25
2 -3 = 0.125
2 -4 = 0.0625
2 -5 = 0.03125
2 -6 = 0.015625
2 -7 = 0.0078125
2 -8 = 0.00390625
0.87510 = 0.?????2
0.87510 = 1 * 2 -1
0.87510 – 0.5 = 0.37510
0.37510 = 1 * 2 -2
0.37510 – 0.25 = 0.12510
0.12510 = 1 * 2 -3
0.12510 – 0.125 = 010
0.
1
1
1
2
21
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
22
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]
23
Real (Decimal) Number Storage
• Numbers have limited precision
– Most real numbers have an infinite decimal
expansion
24
Real Number Storage
Limited 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
25
Real Number Storage
Limited Range and Precision Illustrated
With one bit to the right of the decimal point, only
the real number 0.5 can be represented.
26
Real Number Storage
Limited 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, …
27
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 nonscientific work
28
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
29
Social Themes
Risks 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
30
Software for Numerical Work
• Software Libraries
• Spreadsheets
• Mathematical Software
– symbolic manipulation
– data analysis
– data visualization
31
CSC 101
Overview of Computer Science
Lecture 15
03/02/2004
Numeric Processing
32
Integer Storage
• Integers are typically 32 bits (word size)
• Little-endian: intel
– Most significant byte on the right
LSB
MSB
• Big-endian: SUN, SGI
– Most significant byte on the left
MSB
LSB
33
Integer Storage
• Problem occurs when a binary file is
transferred between different endian
machines
Pow-Wow
34
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
35
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
36
Binary to Decimal Conversion
• Algorithm:
Base 2 powers:
2n 2n-1
…
22 21 20
Binary digits:
1 0
…
0
Multiply columns:
2n
…
Add values:
2n + … + 21 + 20 = number10
1 1
21 20
37
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
Change 2710 to its binary representation
Largest power of 2 less than 2710 is 4 since
24 ≤ 16 < 27 < 32 = 25
27 – 16 = 11
– Place a 1 in the 24 position
38
Decimal to Binary Conversion
• Algorithm:
24 23 22 21 20
1 * * * *
– Repeat until the decimal value is 0
23 = 8 ≤ 11 < 16 = 24
11 - 8 = 3
24 23 22 21 20
1 1 * * *
21 = 2 ≤ 3 < 4 = 22
3–2=1
24 23 22 21 20
1 1 * 1 *
20 ≤ 1
1–1=0
24 23 22 21 20
1 1 * 1 1
39
Decimal to Binary Conversion
• Algorithm:
– Put zeros where there are *’s
2710
24 23 22 21 20
= 1 1 0 1 12
– Can add leading zeros if you like
40
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)
41
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
42
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
43
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
44
Real (Decimal) Number Storage
Byte 0
1
2
3
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
• 32-bit floating point number has less
precision that a 32-bit integer number
• Floating point number has 24-bits resolution
45
Real (Decimal) Number Storage
Byte 0
1
2
3
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
• Can represent
– A large range of floating point numbers
– Numbers to greater precision
• By adding more bytes
46
Real (Decimal) Number Storage
• Double precision floating point numbers
Byte 0
1
2
3
seeeeeee eee f f f f f f f f f f f f f f f f f f f f
ffffffff ffffffff ffffffff ffffffff
Byte 4
5
6
7
– s: (1) sign bit
– e: (11) exponent bits [-1023 … 1024]
– f: (53) fractional part [52 bits + 1 implied
bit]
47
Real (Decimal) Number Storage
• Numbers have limited precision
Compute 1
48
Real Number Storage
Limited 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
49
Real Number Storage
Limited Range and Precision Illustrated
50
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 nonscientific work
51
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
52
Social Themes
Risks 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
53
Software for Numerical Work
• Software Libraries
• Spreadsheets
• Mathematical Software
– symbolic manipulation
– data analysis
– data visualization
54
CSC 101
Overview of Computer Science
Lecture 16
03/02/2004
Numeric Processing
55
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
56
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
57
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
58
Real (Decimal) Number Storage
Byte 0
1
2
3
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
• 32-bit floating point number has less
precision that a 32-bit integer number
• Floating point number has 24-bits resolution
59
Real (Decimal) Number Storage
Byte 0
1
2
3
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
• Can represent
– A large range of floating point numbers
– Numbers to greater precision
• By adding more bytes
60
Real (Decimal) Number Storage
• Double precision floating point numbers
Byte 0
1
2
3
seeeeeee eee f f f f f f f f f f f f f f f f f f f f
ffffffff ffffffff ffffffff ffffffff
Byte 4
5
6
7
– s: (1) sign bit
– e: (11) exponent bits [-1023 … 1024]
– f: (53) fractional part [52 bits + 1 implied
bit]
61
Real (Decimal) Number Storage
• Numbers have limited precision
Compute 1
62
Real Number Storage
Limited 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
63
Real Number Storage
Limited Range and Precision Illustrated
64
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 nonscientific work
65
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
66
Social Themes
Risks 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
67
Software for Numerical Work
• Software Libraries
• Spreadsheets
• Mathematical Software
– symbolic manipulation
– data analysis
– data visualization
68