Handling overflow and negative numbers at the same time in the division algorithm

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Handling overflow and negative numbers at the same time in the division algorithm

Sai krishna
In the implementation notes of the division function in math class, we were told to add an extra condition to check if the divisor is less than zero; if so return zero in order to counter the overflow problem. But what if the input is itself a negative divisor and the dividend is a positive number which is greater than the absolute value of the divisor? Then returning a zero is not the correct solution.
Also, in the same implementation notes, to handle negative numbers input, we were told to find the quotient for the absolute values of the input and apply the sign as needed. But this creates problems during recursion because if when the division function is called with twice the divisor value, then the called division function would calculate the answer for the absolute values of the input where the overflow condition will be mistreated.
How to overcome this problem?