Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMS 161 Introduction to Computing Title: Numeric Processing Date: October 22, 2004 Lecture Number: 24.

Similar presentations


Presentation on theme: "1 COMS 161 Introduction to Computing Title: Numeric Processing Date: October 22, 2004 Lecture Number: 24."— Presentation transcript:

1 1 COMS 161 Introduction to Computing Title: Numeric Processing Date: October 22, 2004 Lecture Number: 24

2 2 Announcements Exam 2 –Monday 10/25/2004 –Covers LANsChapter 4 The InternetChapter 17 HTTP ands HTMLChapter 18 Today’s Material –Chapter 6

3 3 Review Numeric Processing Integers –Magnitude representation –Sign-magnitude representation –Two’s complement numbers

4 4 Outline Numeric Processing –Real numbers

5 5 Magnitude Numbers Use straight decimal to binary conversion, where N 10 = a n * 2 n + a n-1 * 2 n-1 + … a 1 * 2 1 + a 0 * 2 0 The coefficients (a n, a n-1, … a 1, a 0 ) correspond to the binary representation 11 10 = 1*2 3 + 0*2 2 + 1*2 1 + 1*2 0 11 10 = 1 0 1 1

6 6 Magnitude Numbers Can represent 2 n numbers –n is the number of bits { 0, 1, 2, …, 2 n – 1 } n = 0, 2 n = 2 0 = 1, { 0 } n = 1, 2 n = 2 1 = 2, { 0, 1 } n = 2, 2 n = 2 2 = 4, { 0, 1, 2, 3 } n = 3, 2 n = 2 3 = 8, { 0, 1, 2, 3, 4, 5, 6, 7 } Main problem: –No representation for negative numbers

7 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 8 Sign-Magnitude Numbers The positive numbers for n bits { 0, 1, 2, …, ((2 n ) / 2) – 1 } { 0, 1, 2, …, 2 n-1 – 1 } n = 0, 2 n-1 - 1= 2 -1 - 1= -1, {. } –Not even a positive number!!! –Can not represent any numbers with 0 bits!!! n = 1, 2 n-1 – 1 = 2 0 – 1 = 0, { 0 } –Can represent the sign bit with a single bit!!! n = 2, 2 n-1 – 1 = 2 1 – 1 = 1, { 0, 1 } –Two bits, one sign bit one magnitude bit, (2 values)

9 9 Sign-Magnitude Numbers –Positive numbers represented n = 2, 2 n-1 – 1 = 2 1 – 1 = 1, { 0, 1 } n = 3, 2 n-1 – 1 = 2 2 – 1 = 3, { 0, 1, 2, 3 } n = 4, 2 n-1 – 1 = 2 3 – 1 = 7, {0, 1, 2, 3, 4, 5, 6, 7} 000100000101001100000001001000110100010101100111 01 01 0123 234567 000100000101001100000001001000110100010101100111

10 10 Sign-Magnitude Numbers The negative numbers for n bits {-((2 n ) / 2) + 1, -((2 n ) / 2) + 2, … -1, 0 } {-2 n-1 + 1, -2 n-1 + 2, … -1, 0 } n = 0, -2 n-1 + 1 = -2 -1 + 1 = 1 –Not even a negative number!!! –Can not represent any numbers with 0 bits!!! n = 1, -2 n-1 + 1 = -2 0 + 1 = 0, { 0 } –Can represent the sign bit with a single bit!!! n = 2, -2 n-1 + 1 = -2 1 + 1 = -1, { -1, 0 } –Two bits, one sign bit one magnitude bit, (2 values)

11 11 Sign-Magnitude Numbers –Negative numbers represented n = 2, -2 n-1 + 1 = -2 1 + 1 = -1, { -1, -0 } n = 3, -2 n-1 + 1 = -2 2 + 1 = -3, { -3, -2, -1, -0 } n =4,-2 n-1 +1= -2 3 +1 = -7, {-7,-6,-5,-4,-3,-2,-1,-0} 111011111010110011111110110111001011101010011000 -0 -3-2-0 -7 -6-5-4-3-2-0 111011111010110011111110110111001011101010011000

12 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 13 Two’s Complement Numbers Two steps –Invert bits of the magnitude representation of the number –Add one (1) to the result -7 10 Magnitude number: -7 10 Magnitude representation: 0111 Bit inversion: 1000 Add one: 1001 -7 10 in two’s complement is: 1001

14 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 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 16 Two’s Complement Mathematical operations give a correct result -3 – 4 = -3 + -4 = -7 -3 1101 +(-4) +1100 -7 1 0 0 1 1 1 Ignore the carry-out

17 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 18 Real Numbers Scientific Notation –Number times 10 to a power 123456.0 = 123.456 * 10 3 Normalized scientific notation –Zero to the left of the decimal point 123456.0 = 0.123456 * 10 6 Advantageous to store real numbers in 32 bits (word size)

19 19 Real Numbers 2 2 2 1 2 0 2 -1 2 -2 2 -3 2 -4 4 2 1 0.5 0.25 0.125 0.0625 0.101 2 = 0 * 2 0 + 1 * 2 -1 + 0 * 2 -2 + 1 * 2 -3 0.101 2 = 0.5 + 0.125 = 0.625 10 4 2 1 1/2 1/4 1/8 1/16

20 20 Real Numbers 12 10 = 0 * 2 4 + 1 * 2 3 2 4 2 3 2 2 2 1 2 0 16 8 4 2 1 12 10 – 8 10 = 4 10 4 10 = 1 * 2 2 4 10 – 4 10 = 0 10 01100 2 Decimal to binary conversion algorithm

21 21 Real Numbers 2 0 = 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.875 10 – 0.5 = 0.375 10 0.875 10 = 1 * 2 -1 0.375 10 = 1 * 2 -2 0.375 10 – 0.25 = 0.125 10 0.125 10 = 1 * 2 -3 0.125 10 – 0.125 = 0 10 0. 2 1 1 1 0.875 10 = 0.????? 2

22 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 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 24 Real (Decimal) Number Storage Numbers have limited precision –Most real numbers have an infinite decimal expansion

25 25 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

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

27 27 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, …

28 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 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 30 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

31 31 Software for Numerical Work Software Libraries Spreadsheets Mathematical Software –symbolic manipulation –data analysis –data visualization

32 32 CSC 101 Overview of Computer Science Lecture 15 03/02/2004 Numeric Processing

33 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 LSBMSB LSB

34 34 Integer Storage Problem occurs when a binary file is transferred between different endian machines Pow-Wow

35 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 36 Binary to Decimal Conversion Recall positional notation –Position of the digit determines its values 11011011 2 = 1*2 7 + 1*2 6 + 0*2 5 + 1*2 4 + 1*2 3 + 0*2 2 + 1*2 1 + 1*2 0 = 1*128 + 1*64 + 1*16 + 1*8 + 1*2 + 1*1 = 128 + 64 + 16 + 8 + 2 + 1 = 219 10

37 37 Binary to Decimal Conversion Algorithm: Binary digits: 10011 … Base 2 powers: 2n2n 2 n-12 2121 2020 … Multiply columns: 2n2n 2121 2020 … Add values: 2 n + … + 2 1 + 2 0 = number 10

38 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 2 4 position Change 27 10 to its binary representation Largest power of 2 less than 27 10 is 4 since 2 4 ≤ 16 < 27 < 32 = 2 5 27 – 16 = 11

39 39 Decimal to Binary Conversion Algorithm: –Repeat until the decimal value is 02 2121 2020 2424 2323 ***1* 2 3 = 8 ≤ 11 < 16 = 2 4 11 - 8 = 32 2121 2020 2424 2323 ***11 2 1 = 2 ≤ 3 < 4 = 2 2 3 – 2 = 12 2121 2020 2424 2323 *1*11 2 0 ≤ 1 1 – 1 = 02 2121 2020 2424 2323 *1111

40 40 Decimal to Binary Conversion Algorithm: –Put zeros where there are *’s –Can add leading zeros if you like2 2121 2020 2424 2323 011212 1127 10 =

41 41 Real Numbers Scientific Notation –Number times 10 to a power 123456.0 = 123.456 * 10 3 Normalized scientific notation –Single digit to the left of the decimal point 123456.0 = 1.23456 * 10 5 Advantageous to store real numbers in 32 bits (word size)

42 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 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 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 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 seeeeeeee f f f f f f ff f f f Byte 0 1 2 3

46 46 Real (Decimal) Number Storage Can represent –A large range of floating point numbers –Numbers to greater precision By adding more bytes seeeeeeee f f f f f f ff f f f Byte 0 1 2 3

47 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] seeeeeeeeee f f f ff f f f Byte 0 1 2 3 f f f f Byte 4 5 6 7

48 48 Real (Decimal) Number Storage Numbers have limited precision Compute 1

49 49 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

50 50 Real Number Storage Limited Range and Precision Illustrated

51 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 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 53 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

54 54 Software for Numerical Work Software Libraries Spreadsheets Mathematical Software –symbolic manipulation –data analysis –data visualization

55 55 CSC 101 Overview of Computer Science Lecture 16 03/02/2004 Numeric Processing

56 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 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 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 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 seeeeeeee f f f f f f ff f f f Byte 0 1 2 3

60 60 Real (Decimal) Number Storage Can represent –A large range of floating point numbers –Numbers to greater precision By adding more bytes seeeeeeee f f f f f f ff f f f Byte 0 1 2 3

61 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] seeeeeeeeee f f f ff f f f Byte 0 1 2 3 f f f f Byte 4 5 6 7

62 62 Real (Decimal) Number Storage Numbers have limited precision Compute 1

63 63 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

64 64 Real Number Storage Limited Range and Precision Illustrated

65 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 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 67 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

68 68 Software for Numerical Work Software Libraries Spreadsheets Mathematical Software –symbolic manipulation –data analysis –data visualization


Download ppt "1 COMS 161 Introduction to Computing Title: Numeric Processing Date: October 22, 2004 Lecture Number: 24."

Similar presentations


Ads by Google