3 ms·
Let's represent the number 42,643,192, or 10100010101010111011111000₂, in different "floating point" representations. Scientific notation with 5 significant fi
by Veedrac 9y ago
Let's represent the number 42,643,192, or 10100010101010111011111000₂, in different "floating point" representations.
Scientific notation with 5 significant figures:
4.2643 × 10⁷
Scientific notation in base 2 with 17 significant binary figures:
1.0100010101010111₂ × 2²⁵
Let's pack this in a fixed-length datatype. Note that 011001₂ is the binary encoding of 25.
1 0100010101010111 011001
1 mantissa exp.
This doesn't suffice because
a. We're wasting a bit on the leading 1.
b. We want to support negative values.
c. We want to support negative exponents.
d. It would be nice if values of the same sign sorted by their representation.
The leading 1 can be dropped and replaced with a sign bit (0 for "+", 1 for "-"). The exponent can have 100000₂ subtracted from it, so 011001₂ represents 25-32, or -7, and 111001₂ represents 25. Sorting can be handled by putting the exponent before the mantissa.
Thus we get to a traditional floating point representation.
0 111001 0100010101010111
± exp. mantissa
Real floating point has a little more on top (infinities, standardised field sizes, etc.) but is fundamentally the same.
- kibwen 9y ago> Sorting can be handled by putting the exponent before the mantissa. Don't denormal numbers prevent simply sorting this way? I could use a diagram like the OP's to remind myself how they work...
- thethirdone 9y ago> Don't denormal numbers prevent simply sorting this way? I could use a diagram like the OP's to remind myself how they work... No, there is nothing really special about subnormal numbers other than that they have a smaller than normal precision. For example 0x00000001 < 0x00400000 in both floating point and normal integer arithmetic. The only weird bit about sorting floats as integers is that the order is reversed; eg -0f is represented by the smallest integer (0x80000000) and -Inf is represented by 0xFF800000.
- cesarb 9y agoUsing the article's "window" analogy: denormals are in the smallest "window", so the sort order between the "windows" (exponents) is kept. Their special property is that they don't have the implicit one to the left of the point (it's an implicit zero instead); the order within the mantissa is still kept. That is: 0 000000 0100010101010111 ± exp. mantissa Here, the value is 0.0100010101010111₂ × 2^-30 (I hope I calculated the exponent correctly) As a bonus, the zero value comes naturally in this approach: it's a denormal with a mantissa of zero. Without denormals, the implicit one would get in the way.