Solutions to the midterms =========================================================================== Any answer that was mostly correct got at least 1/2 the marks, and any answer that was mostly wrong should got at most 1/2 the marks. Do No .5 marks were given; instead, I gave you a benefit of doubt and assumed that you know what you are doing. QUESTION 1 Part A The largest exponent that can be represented is +99 and the largest mantissa is .999999 (since it must first be normalized). Hence, the largest positive floating-point number in this system is 0.999999 x 10^{+99}; in unnormalized form with no significant bit after the dot, it is 999999^{93}. Part B The largest round-off error occurs when the exponent has the largest value (99) and the number to be represented is exactly halfway between two floating-point numbers. For example, the real number 2.32425 x 10^{98} is represented using either of the floating-point numbers .23242 x 10^{99} or .23243 x 10^{99}, with a round-off error in both cases of .0000005 x 10^{99} = 5 x 10^{92}. Part C After normalizing, we do: 0.0353472 x 10^{+2} (+) 0.35347222 x 10^{+2} = 0.38881922 x 10^{+2} , and 0.38881922 x10^{+2} (+) 0.00000011353472 x 10^{+2} = 0.38881922 x 10^{+2} = 0.388819 x 10^{+2} . Last number doesn't affect the result. In general, add the smallest numbers first. The result of the subtraction is 0.00000022 x 10^{+2} = 0.000000 x 10^{+2} which is .22 x 10^{-4}. This number is representable !. So no problem with it. A couple of other possible answers were accepted: In general, don't subtract two too close numbers; or double the precision; extend the length of the mantissa. These answers are not quite correct (why ?), but I accepted them. QUESTION 2 Part A [a_0,b_0] = [0,1]. m_0 = (a_0+b_0)/2 = 1/2 and f(m_0) = 16^{1/2}-10 = sqrt{16}-10 = 4-10 = -6 < 0 so [a_1,b_1] = [m_0,b_0] = [1/2,1]. m_1 = (a_1+b_1)/2 = 3/4 and f(m_1) = 16^{3/4}-10 = (sqrt[4]{16})^3-10 = 2^3-10 = -2 < 0 so [a_2,b_2] = [m_1,b_1] = [3/4,1]. Part B From part (b), [a_2,b_2] = [3/4,1], so the bisection method will return the approximate root (a_2+b_2)/2 = 7/8 = 0.875. The bound on the error is half the size of the interval [a_2,b_2] which is (b_2-a_2)/2 = 1/8 = 0.125. Note: these values may be different depending on the initial values guessed in Part A. Important is to show your work. Part C The error at step i is no more than {1/2}/{2^i} = 1/2^{i+1}. To guarantee that the error at step i is less than 0.005, we pick i such that 1/2^{i+1} < 0.005, i.e., 1/2^i < 0.01. By solving this for i, we get i > log_2(100) > 6, which is almost 7. QUESTION 3 Part A float secant(float x0, float x1, int it, float tol) { float f0 = f(x0); float f1 = f(x1); int k = 1; float x2; do { /* Compute the secant and update the approximation. */ x2 = x1 - ((x1-x0)/(f1-f0))*f1; x0 = x1; f0 = f1; x1 = x2; f1 = f(x2); k = k+1; } while ((fabs(x2 - x1) >= tol) && (k < it)) /* Error = |x2-x1|, so stop when |x2-x1| < tol. */ return (x2); } /* END secant() */ QUESTION 4 Part A 1. 14 2. 14 3. 54 4. false Part B int (*a)[13] -- Pointer to an array holding 13 integers int *a[13] -- Array of 13 pointers to integers char (*f)() -- pointer to a function returning chars (*p)++ -- value of item at which p is pointing is first evaluated and then incremented Part C p->b = ' '; -- illegal p->e[3] = 10; -- legal (*p).d.a = '*'; -- legal p->d->c = 20; -- illegal Part D %-8d 456 ..... %08d 00000456