#1,089 – How 32-Bit Floating Point Numbers Are Stored in .NET, part II
May 5, 2014 1 Comment
We store the sign, exponent, and mantissa of a binary floating point number in memory as follows.
The sign is stored using a single bit, 0 for positive, 1 for negative.
The exponent is stored using 8 bits. The exponent can be positive or negative, but we store it as a positive value by adding an offset (bias) of 127. We add 127 to the desired exponent and then store the resulting value. Exponents in the range of -126 to 127 are therefore stored as values in the range 1 to 254. (Stored exponent values of 0 and 255 have special meaning).
Because we normalize the binary floating point number, it has the form 1.xxx. Since it always includes the leading 1, we don’t store it, but use 23 bits to store up to 23 digits after the decimal point.