1 coms 161 introduction to computing title: numeric processing date: october 22, 2004 lecture...

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

Upload: percival-miles

Post on 29-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

1

COMS 161Introduction to Computing

Title: Numeric Processing

Date: October 22, 2004

Lecture Number: 24

Page 2: 1 COMS 161 Introduction 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

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

3

Review

• Numeric Processing

• Integers– Magnitude representation– Sign-magnitude representation– Two’s complement numbers

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

4

Outline

• Numeric Processing– Real numbers

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

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

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

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

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

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

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

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)

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

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]

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

24

Real (Decimal) Number Storage

• Numbers have limited precision

– Most real numbers have an infinite decimal expansion

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

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

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

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.

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

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

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

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

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

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

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

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

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

31

Software for Numerical Work

• Software Libraries

• Spreadsheets

• Mathematical Software– symbolic manipulation

– data analysis

– data visualization

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

32

CSC 101Overview of Computer Science

Lecture 1503/02/2004

Numeric Processing

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

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

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

34

Integer Storage

• Problem occurs when a binary file is transferred between different endian machines

Pow-Wow

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

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

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

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

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

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

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

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

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

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

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

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 =

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

48

Real (Decimal) Number Storage

• Numbers have limited precision

Compute 1

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

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

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

50

Real Number StorageLimited Range and Precision Illustrated

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

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

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

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

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

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

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

54

Software for Numerical Work

• Software Libraries

• Spreadsheets

• Mathematical Software– symbolic manipulation

– data analysis

– data visualization

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

55

CSC 101Overview of Computer Science

Lecture 1603/02/2004

Numeric Processing

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

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

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

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

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

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

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

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

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

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

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

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

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

62

Real (Decimal) Number Storage

• Numbers have limited precision

Compute 1

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

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

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

64

Real Number StorageLimited Range and Precision Illustrated

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

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

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

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

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

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

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

68

Software for Numerical Work

• Software Libraries

• Spreadsheets

• Mathematical Software– symbolic manipulation

– data analysis

– data visualization