1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
2 Lecture Template: Floating Point Numbers Floating Point Numbers Exponential Notation Exponential Notation Excess-50 Notation Excess-50 Notation Overflow and Underflow Overflow and Underflow Floating Point Calculations Floating Point Calculations Normalization in Floating Point Normalization in Floating Point IEEE 754 Standard IEEE 754 Standard Packed Decimal Format Packed Decimal Format Programming Considerations Programming Considerations
3 Floating Point Numbers Real numbers Used in computer when the number 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
4 Exponential Notation The representations differ in that the decimal place – the “point” -- “floats” to the left or right (with the appropriate adjustment in the exponent). The following are equivalent representations of 1, ,400.0 x ,340.0 x ,234.0 x x x x x 10 4
5 Exponential Notation Also called scientific notation 4 specifications required for a number 1. Sign (“+” in example) 2. Magnitude or mantissa (12345) 3. Sign of the exponent (“+” in 10 5 ) 4. Magnitude of the exponent (5) Plus 5. Base of the exponent (10) 6. Location of decimal point (or other base) radix point x x x 10 -4
6 Parts of a Floating Point Number x Sign of mantissa Location of decimal point Mantissa Exponent Sign of exponent Base
7 Floating Point Format Specification Integer format (8-bit word) 7 decimal digits and a sign Range: -9,999,999 < I < +9,999,999 Floating point format (8-bit word) Sign of the mantissa SEEMMMMM 2-digit Exponent5-digit Mantissa
8 Format Mantissa: stored in sign-magnitude format Assume decimal point located at the beginning of mantissa Exponent stored in Excess-N notation: Complementary notation Pick middle value as offset where N is the middle value: e.g., excess-50 Representation Exponent being represented – Increasing magnitude+
9 Excess-50 notation Excess-N representation: R = N + EE Example1: N = 50, EE = 38, R = 88 Example2: N = 50, EE = -38, R = 12 Excess-50: Magnitude range
10 Overflow and Underflow Possible for the number to be too large or too small for representation x =
11 Floating Point Format: Excess-50 First digit represents the sign of mantissa 0 is used as a “+“sign 5 is used as a “-“sign (arbitrarily) Two next digits represent exponent in excess-50 Five last digits represent mantissa fixed decimal point located at the beginning
12 Examples = x 10 3 = =– X =– =– x 10 5 =– = x =
13 Normalization Shift numbers left by increasing the exponent until leading zeros eliminated Converting decimal number into standard format 1. 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
14 Example 1: Add exponent x Position decimal point x Already normalized 4. Cut to 5 digits x Convert number Excess-50 exponent Mantissa Sign
15 Example 2: 1255 x Already in exponential form 1255x Position decimal point x Already normalized 4. Add 0 for 5 digits x Convert number
16 Example 3: Exponential notation x Decimal point in position 3. Normalizing x Add 0 for 5 digits x Convert number
17 Floating Point Calculations 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
18 Example Add 2 floating point numbers Align exponents Add mantissas; (1) indicates a carry (1) Carry requires right shift (850) Round Check results = x 10 1 = = x = = In exponential form= x 10 2 Precision lost
19 Multiplication and Division 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 =106 Since 50 added twice, subtract: 106 – 50 =56 Maintaining precision: Normalizing and rounding multiplication
20 Example Multiply 2 numbers x Add exponents, subtract offset – 50 = 49 Multiply mantissas x = Normalize the results Check results = x =0.125 x = x Normalizing and rounding= x 10 -2
21 Floating Point in the Computer Replace digits with “0” and “1” bits Typical floating point format 32 bits provide range ~ to bit exponent = 256 levels Excess-128 notation 23 bits of mantissa: approximately 7 decimal digits of precision
22 IEEE 754 Standard Most common standard for representing floating point numbers Single precision: 32 bits, consisting of... Sign bit (1 bit) Exponent (8 bits) Mantissa (23 bits) Double precision: 64 bits, consisting of… Sign bit (1 bit) Exponent (11 bits) Mantissa (52 bits)
23 Single Precision Format 32 bits Mantissa (23 bits) Exponent (8 bits) Sign of mantissa (1 bit)
24 Double Precision Format 64 bits Mantissa (52 bits) Exponent (11 bits) Sign of mantissa (1 bit)
25 IEEE 754 Standard PrecisionSingle (32 bit) Double (64 bit) Sign1 bit Exponent8 bits11 bits NotationExcess-127Excess-1023 Implied base22 Range to to Mantissa2352 Decimal digits 7 15 Value range to to
26 IEEE 754 Standard 32-bit Floating Point Value Definition ExponentMantissaValue 0 ±0 0 0Not 0 ± x 0.M 1-254Any ± x 1.M 255 ±0±± 255not 0special condition
27 Normalization in Floating Point 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… E.g.: Mantissa: Actual value: Exponent Formatted using Excess-127 notation Base 2 is implied Range: to =
28 Excess Notation: Example Represent exponent of in excess-127 form: = = Representation=
29 Excess Notation: Example Represent exponent of in excess 127 form: = = Representation=
30 Single Precision: Example = – 127 = 3 0 = positive mantissa 2 3 = 14.0 or 2 3 = =14
31 Single Precision: Exercise What decimal value is represented by the following 32-bit floating point number? Answer: Skip answer Answer
32 Single Precision: Exercise Answer What decimal value is represented by the following 32-bit floating point number? Answer:
33 Step by Step Solution To decimal form = * = ( negative )
34 Step by Step Solution : Alternative Method To decimal form = ( negative ) Shift “Point”
35 IBM floating point formats
36 Alpha floating point formats
37 Exercise: Floating Point Conversion Express 3.14 as a 32-bit floating point number Answer: (Note: only use 10 significant bits for the mantissa) Skip answer Answer
38 Exercise: Floating Point Conversion Answer Express 3.14 as a 32-bit floating point number Answer: (Note: only use 10 significant bits for the mantissa)
39 Detail Solution : 3.14 to IEEE double precision 3.14 To Binary (approx): Delete implied left-most “1” and normalize Exponent = position point moved when normalized Value is positive: Sign bit = Prove !
40 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
41 Packed Decimal Format Each decimal digit is stored in BCD Two digits in a byte 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 Decimal point not stored: must be maintained by application software
42 Packed Decimal Format: Example 1 Decimal Value: , unsigned Packed Decimal: Byte 1 Byte 2 Byte 3
43 Packed Decimal Format: Example 2 Decimal Value: Packed Decimal: Byte 1 Byte 2 Byte 3
44 Integer vs. Floating Point: Programming Considerations Integer advantages 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: Short integer (16 bits) Long integer (64 bits)
45 Integer vs. Floating Point: Programming Considerations Real numbers, if: Variable or constant has fractional part Numbers take on very large or very small values outside integer range Program should use least precision sufficient for the task Higher precision formats require more storage Packed decimal attractive alternative for business applications
46 Computer humour