============================================ Notes and answers for Lab 05 (computer math) ============================================ - Ian! D. Allen - idallen@idallen.ca - www.idallen.com Text References: ECOA2e Section 2.3, 2.4, 2.5, 2.5.3, 2.5.5, 2.5.6 Class Notes http://teaching.idallen.com/cst8214/07f/ binary_math.txt Binary Mathematics, unsigned, two's complement, etc. hexadecimal_conversions.txt Converting to/from hexadecimal (base 16) overflow.txt Calculating the OVERFLOW flag in binary arithmetic ieee754_conversions.txt Converting to/from IEEE 754 floating point format You can check your work using any online converter; use Google to find one. e.g. http://www.tonymarston.net/php-mysql/converter.php http://www.h-schmidt.net/FloatApplet/IEEE754.html A quiz is here: http://acc6.its.brooklyn.cuny.edu/~gurwitz/core5/binquiz.html Base 36: http://www.cut-the-knot.org/recurrence/word_primes.shtml Many bases: http://www.cut-the-knot.org/binary.shtml 1.Convert 16-bit unsigned 8000h to decimal 32,768. 8000h = 2**15 (or 8 * 16**3) = 32,768 in two's complement 8000h is a (very) negative number: -32,768 2.Convert 16-bit unsigned A123h to decimal 41,251. 10 * 16**3 + 1 * 16**2 + 2 * 16**1 + 3 * 16**0 = 40960 + 256 + 32 + 3 = 41,251 in two's complement A123h is a negative number: -24,285 3.Convert 16-bit unsigned FFFFh to decimal 65,535. This is the dumb, hard way to convert FFFFh: 15 * 16**3 + 15 * 16**2 + 15 * 16**1 + 15 * 16**0 = 61440 + 3840 + 240 + 15 = 65,535 This is the smart, easy way to convert FFFFh: 0FFFFh + 1 = 10000h = 2**16 (or 1 * 16**4) = 65,536 --> therefore 0FFFFh = 65,536 - 1 = 65,535 4.Circle the positive numbers (16-bit unsigned): all of them 5.Add 16-bit unsigned ABCDh to 7FFFh and give the Result, Carry, and Overflow. Is the result correct? ABCDh + 7FFFh = 2BCCh [carry on] [overflow off] [wrong answer] 6.Add 16-bit unsigned 8A9Ch to ABCDh and give the Result, Carry, and Overflow. Is the result correct? 8A9Ch + ABCDh = 3669h [carry on] [overflow on] [wrong answer] 7.Add 16-bit unsigned 9999h to 4321h and give the Result, Carry, and Overflow. Is the result correct? 9999h + 4321h = DCBAh [no carry] [overflow off] [correct] 8.What happens mathematically to the value of a binary number if you "shift" the bits to the right one place by deleting the rightmost binary digit, e.g. 1100 --> 0110 [divide by base (two), discard remainder; i.e. "half"] 9.What happens to the range of values possible in a word if you increase the word length by one bit, e.g. from eight bits to nine bits or from 100 bits to 101 bits? [multiply by base (two); i.e. "doubles"] 10. What happens to the value of a binary number if you "shift" the bits to the left two places by adding two zeros after the rightmost binary digit, e.g. 11001 > 1100100 [multiply by the base (two) twice -> multiply by four] 11. What happens to the value of an octal number if you "shift" the number to the left one place by adding one zero after the rightmost octal digit, e.g. 0377 > 03770 [multiply by the base (eight) once] 12. What happens to the value of a hexadecimal number if you "shift" the number to the left one place by adding one zero after the rightmost hex digit, e.g. 0xABC > 0xABC0 [multiply by the base (16) once] 13.Convert decimal 147.625 to IEEE 754 single-precision format 4313A000h 14.Convert decimal 128.5625 to IEEE 754 single-precision format 43009000h 15.Convert decimal 2004 to IEEE 754 single-precision format 44FA8000h 16.Convert decimal -20.5 to IEEE 754 single-precision format C1A40000h 17.Convert decimal -0.5 to IEEE 754 single-precision format BF000000h 18.Convert decimal -1 to IEEE 754 single-precision format BF800000h 19.Convert IEEE 754 single-precision format 438F0000h to decimal 286 20.Convert IEEE 754 single-precision format BF880000h to decimal -1.0625 21.The IEEE 754 floating-point number 81234567h is negative. Without converting, give the hexadecimal for the same number, only positive. 22.The IEEE 754 floating-point number 7EDCBA98h is positive. Without converting, give the hexadecimal for the same number, only negative. 23.Without converting, circle all the IEEE 754 negative numbers: - See Class Notes ieee754_conversions.txt for all the above answers 24.In the simplified floating-point model used in the text, the significand can only store eight bits of precision. Why can't the decimal value 128.5 be accurately represented in eight bits? (Section 2.5.3) [128.5 needs nine bits of precision; the low bit is lost in 8 bits] 25.IEEE 754 single-precision floating-point can store numbers in the approximate range of 2**-127 to 2**+127. Look up or use a calculator to express this range (approximately) as powers of ten (decimal). [approx. -10**38 to -10**-38 and +10**-38 up to +10**38] [The above question is badly worded - a "range" is from the smallest negative number to the largest positive number. The range should be given above as -2**127 to +2**127 or -10**38 to +10**38 (approximately).] 26.What is the approximate decimal range (powers of ten) of IEEE 754 double-precision floating-point numbers (Figure 2.3, p.70)? [approx. -10**308 up to +10**308] 27.What is floating-point overflow? (p.70, Chapter 2 Slide 81) [the desired value has an exponent greater than the largest allowed exponent, i.e. the magnitue of the number is too big, e.g. 1.0 x 10**50] 28.What is floating-point underflow? (p.70, Chapter 2 Slide 81) [the desired value has an exponent smaller than the smallest allowed exponent, i.e. the number is too close to zero, e.g. 1.0 x 10**-50] 29.What serious mathematical error can occur due to floating-point underflow? (Chapter 2 Slide 81) [divide by zero] 30.Give a decimal example of a floating-point number that would cause overflow if you tried to represent it as an IEEE 754 single-precision floating-point number: [anything significantly over 10**38, e.g. 10**40 or -10**40. Note that since these are decimal approximations, (10**38)+1 isn't sufficiently large to cause overflow, though it will lose precision.] 31.Give an example of a floating-point number that would cause underflow if you tried to represent it as an IEEE 754 single-precision floating-point number: [anything significantly closer to zero than 10**-38, e.g. 10**-40] 32. Decimal 1234.0 x 10**37 fits in IEEE 754 single precision floating point. [False. 1.234 x 10**40 is too big - floating-point overflow] 33. Decimal 0.00001 x 10**40 fits in IEEE 754 single precision floating point. [True. 1.0 x 10**35 is easily less than approx. 10**38] 34. Circle the values that fit in a 32bit two's complement integer with no loss of range or precision: 2**30-3 2**30-1 2**30 2**30+1 2**30+3 2**30+2**29 [all values fit without loss of range or precision] 35. Circle the values that fit in IEEE 754 single precision floating point with no loss of range or precision: 2**30-3 2**30-1 2**30 2**30+1 2**30+3 2**30+2**29 [only 2**30 and 2**30+2**29 do not lose precision] 36. Without converting, circle the sums that fit in IEEE 754 single-precision floating-point with no loss of range or precision: 2**29+2**10+2**9+2**0 2**26+2**0 2**29+2**28+2**27+2**26 2**27+2**23+2**1 2**29+2**28+2**2+2**1 [only 2**29+2**28+2**27+2**26 does not lose precision] 37.Why do the decimal numbers 2147483775 (0x8000007F) and 2147483648 (0x80000000) both convert to the same IEEE 754 single-precision floating-point number 0x4F000000 that has decimal value 2147483648.0? (Hint: For a similar reason, in Section 2.5.3, the numbers 128 and 128.5 both convert to 128.0 when stored in the simplified floating-point format used in the text.) [0x8000007F requires 32 bits of precision; the last (rightmost) 9 bits are thrown away when converting to IEEE, so the "7F" part of 0x8000007F disappears and it looks just like 0x800000] 38.True - floating point mathematics may not be associative or distributive. (Section 2.5.6)