Hi,
SpyderTL wrote:
Do you guys see any major problems with this approach, or do you have any suggestions to improve this?
Every valid floating point number (excluding NaN, infinity, etc) can be displayed with 100% accuracy relatively simply, by adjusting the exponent to ensure that the significand can be displayed as an integer.
For example, assume you've got the "raw hex dword" 0x414587D0 (which is the float representing the value 12.3456573486328125). You can:
- extract the sign: 0x414587DD >> 31 = 0 = positive
- extract the significand bits: 0x414587D0 & 0x007FFFFF = 0x004587D0
- extract the exponent: (0x414587D0 >> 23) & 0xFF = 0x82
- insert the implied bit into the significand: 0x004587D0 | 0x00800000 = 0x00C587D0 = 12945360
- subtract the bias from the exponent: 0x82 - 0x7F = 3
- subtract the significand's width (23 bits) from the exponent: 3 - 23 = -20
At this point you can display the significand and exponent as plain old integers (e.g. "12945360 * 2**-20"). This is 100% accurate.
However, you can simplify it slightly using a loop like this:
Code:
while( (significand & 1) == 0 ) {
significand = significand >> 1;
exponent = exponent + 1;
}
For the example number I chose, the significand becomes 809085 and exponent becomes -16, so you'd display "809085 * 2**-16". This is still 100% accurate.
Of course you will need to detect things like NaN, infinities, zero and denormal numbers first. For displaying denormals (not including either zero), you can do almost the same thing as normal numbers except you don't insert the implied bit into the significand.
Cheers,
Brendan