Hello , all I am trying to implement a floating point arithmetic library and I have trouble understanding the algorithm of subtracting floats. I have implemented addition succesfully and I thought that subtraction was just a special case of it but it seems I am making a mistake somewhere. I am adding the code here just for reference, it has many self explanatory functions but I don't expect someone to understand it 100%. What I would like help with is the algorithm. We follow the same method as with adding float numbers except, when we add the mantissas, we convert the negative one(the one we subtract) into two's complement and then add them?
That's what I am doing but the result is not correct. Albeit it is very close ... but not the same. Anyone has any ideas? Thanks in advance!
I am quite sure that the way I do things works since I implemented an almost identical algorithm for adding floats and it works like a charm.
_float subFloat(_float f1,_float f2)
{
unsigned char diff;
_float result;
//first see whose exponent is greater
if(f1.float_parts.exponent > f2.float_parts.exponent)
{
diff = f1.float_parts.exponent - f2.float_parts.exponent;
//now shift f2's mantissa by the difference of their exponent to the right
//adding the hidden bit
f2.float_parts.mantissa = ((f2.float_parts.mantissa)>>1) | (0x01<<22);
f2.float_parts.mantissa >>= (int)(diff);//was (diff-1)
//also increase its exponent by the difference shifted
f2.float_parts.exponent = f2.float_parts.exponent + diff;
}
else if(f1.float_parts.exponent < f2.float_parts.exponent)
{
diff = f2.float_parts.exponent - f1.float_parts.exponent;
result = f1;
f1 = f2; //swap them
f2 = result;
//now shift f2's mantissa by the difference of their exponent to the right
//adding the hidden bit
f2.float_parts.mantissa = ((f2.float_parts.mantissa)>>1) | (0x01<<22);
f2.float_parts.mantissa >>= (int)(diff);
//also increase its exponent by the difference shifted
f2.float_parts.exponent = f2.float_parts.exponent + diff;
}
else//if the exponents were equal
f2.float_parts.mantissa = ((f2.float_parts.mantissa)>>1) | (0x01<<22); //bring out the hidden bit
//getting two's complement of f2 mantissa
f2.float_parts.mantissa ^= 0x7FFFFF;
f2.float_parts.mantissa += 0x01;
result.float_parts.exponent = f1.float_parts.exponent;
result.float_parts.mantissa = (f1.float_parts.mantissa +f2.float_parts.mantissa)>>1;
//gotta shift right by overflow bits
//normalization
if(manBitSet(result,1))
result.float_parts.mantissa <<= 1; //hide the hidden bit
else
result.float_parts.exponent +=1;
return result;
}