I've been trying to implement floating point addition.
I believe I understand the principle:
- acquire the sign, exponent, and mantissa from each float
- compare which float has a higher exponent
- right shift the mantissa of the lower exponent float the difference of the exponents
- add that mantissa to the mantissa of the higher exponent number. If a carry were to happen (bit 23 would be occupied by the mantissa), increase the exponent of the result by 1.
If a number isn't 0, also take into consideration the hidden 1 that would precede the mantissa when shifting
This does work for strictly positive numbers. But what about negatives?
Does adding 1 and -1 ( 1 + -1)
require to do a subtraction? what about (-1 + -1)
or (10 + -1)
?
How do I take the sign into consideration when working out the algorithm?