ITEC 1000 Introduction to Information Technology Lecture 5

  • Slides: 46
Download presentation
ITEC 1000 “Introduction to Information Technology” Lecture 5 Floating Point Numbers 1

ITEC 1000 “Introduction to Information Technology” Lecture 5 Floating Point Numbers 1

Lecture Template: Floating Point Numbers n Exponential Notation n Excess-50 Notation n Overflow and

Lecture Template: Floating Point Numbers n Exponential Notation n Excess-50 Notation n Overflow and Underflow n Floating Point Calculations n Normalization in Floating Point n IEEE 754 Standard n Packed Decimal Format n Programming Considerations n 2

Floating Point Numbers Real numbers n Used in computer when the number n is

Floating Point Numbers Real numbers n Used in computer when the number n is outside the integer range of the computer (too large or too small) contains a decimal fraction the range in PC’s: r or more 3

Exponential Notation n The following are equivalent representations of 1, 234 123, 400. 0

Exponential Notation n The following are equivalent representations of 1, 234 123, 400. 0 x 10 -2 12, 340. 0 x 10 -1 1, 234. 0 x 100 123. 4 x 101 12. 34 1. 234 x 102 x 103 The representations differ in that the decimal place – the “point” -- “floats” to the left or right (with the appropriate adjustment in the exponent). 0. 1234 x 104 4

Exponential Notation n n Also called scientific notation 12345 x 100 0. 12345 x

Exponential Notation n n Also called scientific notation 12345 x 100 0. 12345 x 105 123450000 x 10 -4 4 specifications required for a number 1. 2. 3. 4. n 12345 Sign (“+” in example) Magnitude or mantissa (12345) Sign of the exponent (“+” in 105) Magnitude of the exponent (5) Plus 5. 6. Base of the exponent (10) Location of decimal point (or other base) radix point 5

Parts of a Floating Point Number -0. 9876 x Sign of mantissa Location of

Parts of a Floating Point Number -0. 9876 x Sign of mantissa Location of decimal point Mantissa -3 10 Exponent Sign of exponent Base 6

Floating Point Format Specification n Integer format (8 -bit word) n n n 7

Floating Point Format Specification n Integer format (8 -bit word) n n n 7 decimal digits and a sign Range: -9, 999 < I < +9, 999 Floating point format (8 -bit word) Sign of the mantissa SEEMMMMM 2 -digit Exponent 5 -digit Mantissa 7

Format n n n Mantissa: stored in sign-magnitude format Assume decimal point located at

Format n n n Mantissa: stored in sign-magnitude format Assume decimal point located at the beginning of mantissa Exponent stored in Excess-N notation: Complementary notation n Pick middle value as offset where N is the middle value: 0. . 99 e. g. , excess-50 Representation 0 49 50 99 Exponent being represented -50 -1 0 49 – Increasing magnitude + 8

Excess-50 notation Excess-N representation: R = N + EE n Example 1: N =

Excess-50 notation Excess-N representation: R = N + EE n Example 1: N = 50, EE = 38, R = 88 n Example 2: N = 50, EE = -38, R = 12 n Excess-50: Magnitude range n 9

Overflow and Underflow n Possible for the number to be too large or too

Overflow and Underflow n Possible for the number to be too large or too small for representation 0. 00001 x 10 -50 = 10 -55 10

Floating Point Format: Excess-50 n First digit represents the sign of mantissa 0 is

Floating Point Format: Excess-50 n First digit represents the sign of mantissa 0 is used as a “+“sign n 5 is used as a “-“sign (arbitrarily) n Two next digits represent exponent in excess-50 n Five last digits represent mantissa n n fixed decimal point located at the beginning 11

Examples 05324567 = 0. 24567 x 103 = 246. 57 54810000 = – 0.

Examples 05324567 = 0. 24567 x 103 = 246. 57 54810000 = – 0. 10000 X 10 -2 = – 0. 0010000 5555555 = – 0. 55555 x 105 = 04925000 = 0. 25000 x 10 -1 = – 55555 0. 025000 12

Normalization n n Shift numbers left by increasing the exponent until leading zeros eliminated

Normalization n n Shift numbers left by increasing the exponent until leading zeros eliminated Converting decimal number into standard format Provide number with exponent (0 if not yet specified) 2. Increase/decrease exponent to shift decimal point to proper position 3. Decrease exponent to eliminate leading zeros on mantissa 4. Correct precision by adding 0’s or discarding/rounding least significant digits 1. 13

Example 1: 246. 8035 1. Add exponent 246. 8035 x 100 2. Position decimal

Example 1: 246. 8035 1. Add exponent 246. 8035 x 100 2. Position decimal point . 2468035 x 103 3. Already normalized 4. Cut to 5 digits 5. Convert number . 24680 x 103 05324680 Sign Excess-50 exponent Mantissa 14

Example 2: 1255 x 10 -3 1. Already in exponential form 2. Position decimal

Example 2: 1255 x 10 -3 1. Already in exponential form 2. Position decimal point 3. Already normalized 4. Add 0 for 5 digits 5. Convert number 1255 x 10 -3 0. 1255 x 10+1 0. 12550 x 10+1 05112550 15

Example 3: - 0. 00000075 1. Exponential notation - 0. 00000075 x 100 2.

Example 3: - 0. 00000075 1. Exponential notation - 0. 00000075 x 100 2. Decimal point in position 3. Normalizing - 0. 75 x 10 -6 4. Add 0 for 5 digits - 0. 75000 x 10 -6 5. Convert number 54475000 16

Floating Point Calculations n Addition and subtraction Exponent and mantissa treated separately Exponents of

Floating Point Calculations n Addition and subtraction Exponent and mantissa treated separately Exponents of numbers must agree • Align decimal points • Least significant digits may be lost Mantissa overflow requires exponent again shifted right 17

Example Add 2 floating point numbers 05199520 + 04967850 Align exponents 05199520 0510067850 Add

Example Add 2 floating point numbers 05199520 + 04967850 Align exponents 05199520 0510067850 Add mantissas; (1) indicates a carry (1)0019850 Carry requires right shift 05210019(850) Round 05210020 Check results 05199520 = 0. 99520 x 101 = 9. 9520 04967850 = 0. 67850 x 10 -1 = 0. 06785 Precision lost = 10. 01985 In exponential form = 0. 1001985 x 102 18

Multiplication and Division n n Mantissas: multiplied or divided Exponents: added or subtracted Normalization

Multiplication and Division n n Mantissas: multiplied or divided Exponents: added or subtracted Normalization necessary to Restore location of decimal point Maintain precision of the result Adjust excess value since added twice Example: 2 numbers with exponent = 53 represented in excess-50 notation 53 + 53 =106 Since 50 added twice, subtract: 106 – 50 =56 n Maintaining precision: Normalizing and rounding multiplication 19

Example Multiply 2 numbers Add exponents, subtract offset Multiply mantissas Normalize the results x

Example Multiply 2 numbers Add exponents, subtract offset Multiply mantissas Normalize the results x 05220000 04712500 52 + 47 – 50 = 49 0. 20000 x 0. 12500 = 0. 025000000 04825000 Check results 05220000 = 0. 20000 x 102 04712500 = 0. 125 x 10 -3 = 0. 0250000000 x 10 -1 Normalizing and rounding 0. 25000 = x 10 -2 20

Floating Point in the Computer n n Replace digits with “ 0” and “

Floating Point in the Computer n n Replace digits with “ 0” and “ 1” bits Typical floating point format 32 bits provide range ~10 -38 to 10+38 8 -bit exponent = 256 levels • Excess-128 notation 23 bits of mantissa: approximately 7 decimal digits of precision 21

IEEE 754 Standard n Most common standard for representing floating point numbers n Single

IEEE 754 Standard n Most common standard for representing floating point numbers n Single precision: 32 bits, consisting of. . . Sign bit (1 bit) Exponent (8 bits) Mantissa (23 bits) n Double precision: 64 bits, consisting of… Sign bit (1 bit) Exponent (11 bits) Mantissa (52 bits) 22

Single Precision Format 32 bits Mantissa (23 bits) Exponent (8 bits) Sign of mantissa

Single Precision Format 32 bits Mantissa (23 bits) Exponent (8 bits) Sign of mantissa (1 bit) 23

Double Precision Format 64 bits Mantissa (52 bits) Exponent (11 bits) Sign of mantissa

Double Precision Format 64 bits Mantissa (52 bits) Exponent (11 bits) Sign of mantissa (1 bit) 24

IEEE 754 Standard Precision Single (32 bit) Double (64 bit) Sign 1 bit Exponent

IEEE 754 Standard Precision Single (32 bit) Double (64 bit) Sign 1 bit Exponent 8 bits 11 bits Notation Excess-127 Excess-1023 2 2 2 -126 to 2127 2 -1022 to 21023 Mantissa 23 52 Decimal digits 7 15 10 -45 to 1038 10 -300 to 10300 Implied base Range Value range 25

IEEE 754 Standard n 32 -bit Floating Point Value Definition Exponent Mantissa Value 0

IEEE 754 Standard n 32 -bit Floating Point Value Definition Exponent Mantissa Value 0 ± 0 0 0 Not 0 ± 2 -126 x 0. M 1 -254 Any ± 2 -127 x 1. M 255 ± 0 ± 255 not 0 special condition 26

Normalization in Floating Point n Mantissa: Must always start with “ 1” Leading bit

Normalization in Floating Point n Mantissa: Must always start with “ 1” Leading bit is not stored Implied that it is located to the left of the binary point Normalized Form: 1. MMMMMMM… n n n E. g. : Mantissa: Actual value: 1010000000000 1. 1012 = 1. 62510 Exponent Formatted using Excess-127 notation Base 2 is implied Range: 2 -126 to 2127 27

Excess Notation: Example Represent exponent of 1410 in excess-127 form: 12710 = + 011111112

Excess Notation: Example Represent exponent of 1410 in excess-127 form: 12710 = + 011111112 1410 = + 000011102 Representation = 100011012 14110 28

Excess Notation: Example Represent exponent of -810 in excess 127 form: 12710 = +

Excess Notation: Example Represent exponent of -810 in excess 127 form: 12710 = + 011111112 - 810 = - 000010002 Representation = 01112 11910 29

Single Precision: Example 0 10000010 1100000000000 1. 112 = 1. 7510 130 – 127

Single Precision: Example 0 10000010 1100000000000 1. 112 = 1. 7510 130 – 127 = 3 0 = positive mantissa +1. 75 23 = 14. 0 or +1. 112 23 = +1110. 0 =14 30

Single Precision: Exercise n What decimal value is represented by the following 32 -bit

Single Precision: Exercise n What decimal value is represented by the following 32 -bit floating point number? 1 10000010 111101100000000 n Answer: Skip answer Answer 31

Single Precision: Exercise Answer n What decimal value is represented by the following 32

Single Precision: Exercise Answer n What decimal value is represented by the following 32 -bit floating point number? 1 10000010 111101100000000 n Answer: -15. 6875 32

Step by Step Solution 1 10000010 111101100000000 To decimal form 130 - 127 =

Step by Step Solution 1 10000010 111101100000000 To decimal form 130 - 127 = 3 1. 11110110000000000 1 +. 5 +. 25 +. 125 +. 0625 + 0 +. 015625 +. 0078125 23 * 1. 9609375 ( negative ) = 15. 6875 - 15. 6875 33

Step by Step Solution : Alternative Method 1 10000010 111101100000000 To decimal form 130

Step by Step Solution : Alternative Method 1 10000010 111101100000000 To decimal form 130 - 127 = 3 1. 11110110000000000 Shift “Point” 1111. 10110000000000 ( negative ) - 15. 6875 34

IBM floating point formats 35

IBM floating point formats 35

Alpha floating point formats 36

Alpha floating point formats 36

Exercise: Floating Point Conversion n Express 3. 14 as a 32 -bit floating point

Exercise: Floating Point Conversion n Express 3. 14 as a 32 -bit floating point number Answer: n (Note: only use 10 significant bits for the mantissa) n Skip answer Answer 37

Exercise: Floating Point Conversion Answer n Express 3. 14 as a 32 -bit floating

Exercise: Floating Point Conversion Answer n Express 3. 14 as a 32 -bit floating point number Answer: 0 10000000 10010001111000000 n (Note: only use 10 significant bits for the mantissa) n 38

Detail Solution : 3. 14 to IEEE double precision 3. 14 To Binary (approx):

Detail Solution : 3. 14 to IEEE double precision 3. 14 To Binary (approx): Delete implied left-most “ 1” and normalize Exponent = 127 + 1 position point moved when normalized 11. 001000111101 Prove ! 1001000111101 10000000 Value is positive: Sign bit = 0 0 10000000 100100011110100000 39

Packed Decimal Format Limited use: e. g: where precision particularly important, as in accounting

Packed Decimal Format Limited use: e. g: where precision particularly important, as in accounting and business functions. Similar to BCD: e. g: four bit representation, as in BCD. -> Stores two digits per byte. Supported by business-oriented languages like COBOL Implemented in IBM System 370/390 and Compaq Alpha 40

Packed Decimal Format n Each decimal digit is stored in BCD Two digits in

Packed Decimal Format n Each decimal digit is stored in BCD Two digits in a byte n n n The most significant digit – stored first, in the high-order bits of the first byte Can store up to 31 digits in 16 bytes The sign is stored in the low-order bits of the last byte Binary 1100 represents “+” Binary 1101 represents “-” Binary 1111 represents unsigned number n Decimal point not stored: must be maintained by application software 41

Packed Decimal Format: Example 1 Decimal Value: 1 0 3 5 7, unsigned Packed

Packed Decimal Format: Example 1 Decimal Value: 1 0 3 5 7, unsigned Packed Decimal: 0001 0000 0011 0101 0111 1111 Byte 2 Byte 3 42

Packed Decimal Format: Example 2 Decimal Value: - 90 4 1 3 Packed Decimal:

Packed Decimal Format: Example 2 Decimal Value: - 90 4 1 3 Packed Decimal: 1001 0000 0100 0001 0011 1101 Byte 2 Byte 3 43

Integer vs. Floating Point: Programming Considerations n Integer advantages n n n Easier for

Integer vs. Floating Point: Programming Considerations n Integer advantages n n n Easier for computer to perform Potential for higher precision Faster to execute Fewer storage locations to save time and space Most high-level languages provide 2 or more different integer word sizes/formats: n n Short integer (16 bits) Long integer (64 bits) 44

Integer vs. Floating Point: Programming Considerations n Real numbers, if: Variable or constant has

Integer vs. Floating Point: Programming Considerations n Real numbers, if: Variable or constant has fractional part n Numbers take on very large or very small values outside integer range n Program should use least precision sufficient for the task n Higher precision formats require more storage n Packed decimal attractive alternative for business applications n 45

Computer humour 46

Computer humour 46