CSCI 402: Computer Architectures Arithmetic for Computers (3) Fengguang Song Department of Computer & Information Science IUPUI 3.5 Today s Contents Floating point numbers: 2.5, 10.1, 100.2, etc.. How can computers support these real numbers? 4 1
Concept of Floating Point Numbers Used to represent Real Numbers (non-integers) Also used to represent very small and very large numbers Scientific notation: A number has 1 single digit to the left of the decimal point Normalized: A number in scientific notation and it has no leading 0s. Some examples? normalized 2.34 10 56 +0.002 10 4 +987.02 10 9 not normalized Apply the same notation to binary numbers: ±1.xxxxxxx 2 2 yyyy Types float and double in C (or mantissa) significand Always the same format. Can simplify floating point arithmetic algorithms 5 IEEE Floating Point Standard There is a compromise between fraction and exponent bits for precision V bits for range However, we only have 32 or 64 bits More precision is always possible, but up to a cost Defined by IEEE Standard 754-1985 Developed in response to divergence of representations Resolve portability issues for scientific code Now, it is universally adopted. Two representations: Single precision floating point (32-bit) Double precision floating point (64-bit) 6 2
IEEE Floating-Point Format single: 8 bits double: 11 bits single: 23 bits double: 52 bits x = ( 1) S (1+) 2 [0, 255] (stored Exponent Bias) S: sign bit (0 Þ non-negative, 1 Þ negative) Normalized significand: 1.0 significand < 2.0 Always has a leading (pre-binary-point) 1, so no need to represent it explicitly ( hidden bit ) Significand = with the 1. restored Stored exponent = actual exponent + Bias Actual exponent = stored exponent - Bias Stored exponent is unsigned: e.g., 0 to 255, or 0 to 2047 Single: Bias = 127; Double: Bias = 1023 7 Single-Precision Range First of all: infinity Exponent of 0000 0000 and 1111 1111 are reserved The smallest value (closest to zero)? Exponent: 0000 0001 Þ actual exponent = 1 127 = 126 : 000 00 Þ significand = 1.0 ±1.0 2 126 ±1.2 10 38 The largest value (furthest from zero)? exponent: 1111 1110 Þ actual exponent = 254 127 = +127 : 111 11 Þ significand 2.0 ±2.0 2 +127 ±3.4 10 +38 8 3
Double-Precision Range Again, exponents 0000 00 and 1111 11 are reserved Smallest value Exponent: 000 0000 0001 Þ actual exponent = 1 1023 = 1022 : 000 00 Þ significand = 1.0 ±1.0 2 1022 ±2.2 10 308 Largest value Exponent: 111 1111 1110 Þ actual exponent = 2046 1023 = +1023 : 111 11 Þ significand 2.0 ±2.0 2 +1023 ±1.8 10 +308 Main advantage: much larger-size fraction! 9 Overflow and Underflow When a number becomes too big to be represented by the exponent field à Overflow i.e., > 2.0 2 +127 (float) When a number becomes too small to be represented in the exponent field à Underflow between ±1.0 2 126 (float) 10 4
IEEE 754 Encoding of Floating Point Numbers 0 is a special case in the IEEE 754 Std. It seems we have no way to represent 1.0, which would be 1.0x2 0 (an exponent of zero, times the hidden one)! 11 Denormalized Numbers Exponent = 000...0 Þ hidden bit is 0, i.e. S -Bias x = (-1) (0+ ) 2 n Smaller than normalized 1.xxxx numbers n Allow for gradual underflow n Denormalized number with fraction = 000...0 x = (-1) S (0+ 0) 2 Two representations of 0.0! -Bias = ±0.0 12 5
Representation of Infinities and NaNs Exponent = 111...1, = 000...0 ±Infinity e.g., 1/0 Can be used in subsequent calculations, avoiding the need for overflow check Exponent = 111...1, 000...0 Not-a-Number (NaN) Indicates illegal or undefined result e.g., 0.0 / 0.0, Inf - Inf Can still be used in subsequent calculations 13 Floating-Point# s Precision Precision: Minimum difference between two floating point numbers? For single (23-bit fraction): approximately 2 23 Equivalent to 23 log 10 2 23 0.3 6 decimal digits of precision Not sufficient for scientific computing! For double (52-bit fraction): approximately 2 52 Equivalent to 52 log 10 2 52 0.3 16 decimal digits of precision also called machine epsilon Minimum epsilon s.t. 1 + epsilon > 1. 14 6
Accuracy vs Precision Accuracy is how close a measured value is to the true value. Precision is how close the measured values are to each other. Low accuracy High precision High accuracy Low precision High accuracy High precision 15 7