Question: You are given the assignment to develop code for a function tmult_ok that will determine whether two arguments can be multiplied without causing overflow. Here
You are given the assignment to develop code for a function tmult_ok that will determine whether two arguments can be multiplied without causing overflow. Here is your solution:
You test this code for a number of values of x and y, and it seems to work properly. Your coworker challenges you, saying, "If I can't use subtraction to test whether addition has overflowed (see Problem 2.31), then how can you use division to test whether multiplication has overflowed?" Devise a mathematical justification of your approach, along the following lines. First, argue that the case x = 0 is handled correctly. Otherwise, consider w-bit numbers x (x ≠ 0), y, p, and q, where p is the result of performing two's complement multiplication on x and y, and q is the result of dividing p by x.
Problem 2.31
Your coworker gets impatient with your analysis of the overflow conditions for two’s-complement addition and presents you with the following implementation of tadd_ok:

You look at the code and laugh. Explain why.
/* Determine whether arguments can be multiplied without overflow */ int tmult_ok (int x, int y) { } int p = x*y; /* Either x is zero, or dividing p by x gives y */ return !x p/x == y;
Step by Step Solution
3.48 Rating (148 Votes )
There are 3 Steps involved in it
It is not realistic to test this function for all possible values of x and y Even if you could run 1... View full answer
Get step-by-step solutions from verified subject matter experts
