r/ProgrammerTIL • u/mikaey00 • May 16 '19
Other TIL learned how floating-point numbers are represented in binary form.
I'm 37 now, and I've been doing C since I was maybe 14. I never quite understood the binary format of floating point numbers, so finally I sat down and managed to find something that explained it to me. With that, I was able to write the following pseudocode to decode a floating-point number (the example below is for a 32-bit float):
Sign = FloatVal >> 31; // Bit 0
Exponent = ( FloatVal >> 23 ) & 0x7f; // Bits 1-8
Mantissa = FloatVal & 0x7fffff; // Bits 9-31
if( Exponent == 255 ) {
if( Mantissa == 0 ) {
return ( Sign == 1 ? -Infinity : Infinity );
} else {
return ( Sign == 1 ? -NaN : NaN );
}
} else {
if( Exponent != 0 ) {
return ( Sign == 1 ? -1 : 1 ) * ( 1 + ( Mantissa / 0x800000 ) ) * 2^( Exponent - 127 );
} else {
return ( Sign == 1 ? -1 : 1 ) * ( Mantissa / 0x800000 ) * 2^-126;
}
}Thank you to Bruce Dawson's blog that explained this nicely!
164
Upvotes
View all comments
Show parent comment
15
u/mikaey00 May 16 '19
Yeah, I knew of IEEE 754, but couldn't quite get the hang of it -- cause I couldn't quite grasp how the mantissa was encoded. I had a hard time finding anything that explained it in an easy-to-understand fashion. It wasn't until I found Bruce Dawson's blog and read through it that I finally understood the "you have to take the mantissa and divide by 0x800000 to get the fractional part" part.