Originally Posted by
DanV2
Right, like iMalc mentioned, the while loops are for normalizing the mantissa of the result. The fraction part of an IEEE Floating point number should be 23 bits with an implied 1 bit, so the while loops shift right or left as necessary to normalize the result.
As for shifting the two FRAC values to the right, I'm not completely sure why we do, but the TA told us that it was necessary to truncate the values so that the result would fit into an integer. At least, that's what I remember from the explanation he gave.
While it's obvious that you're trying to simulate the results of floating point division, there's no need to shift the two mantissas to the right. The thing that needs to be done tho' is right shifting the lesser of the two fractions in order to equalise them. After equalising, if numerator <= denominator, repeatedly multiply it by 10 until it becomes just greater than the denominator, specifically:
Code:
1 / 5
frac1 = 1 (1.0 x 2^0 - binary)
frac2 = 5 (1.01 x 2^2 - binary)
frac1 >>= 2; // equalise the magnitudes of the two fractions
while (frac1 < frac2)
frac1 *= 10;
And no need to store the result of FRAC1 / FRAC2 in a float, as it can obtained thro' integer division and modulus.