问题
I am trying to understand floating point representations. I do not understand the 'infinity' 'NaN' representations in floating point. I am looking at the table provided by TopCoder.
Infinity is represented by an exponent of all 1s and a Mantissa of all 0s.
I can only read it as 0^n, and I'm not sure how that tends to infinity.
回答1:
You should simply ignore the bit patterns used and treat infinities and NaNs as special values. Unless you do bit-fiddling on your floats (for which I see very little reason to do), you will only encounter these values in the places where the IEEE rules say they should be created, and it will be safe to ignore the question of how the computer represents them. Even if you do more than standard calculations, my guesstimate is that 99% of the time, the bit patterns just don't matter.
Of course, none of the above says you should ignore the implementation of standard, finite, non-zero floating point numbers. Although the step from mantissa/exponent to bits in memory is still hardly ever relevant. In the page you linked to, the author claims that “global variables in C++ are initialized to a zero bit pattern” – that is wrong. Global variables of POD type in C++ are initialized with whatever bit pattern is needed for a semantic zero (§3.6.2.2, §8.5.5). I believe the C++ standard does not require any particular bit pattern for any floating-point value.
来源:https://stackoverflow.com/questions/10528730/why-is-infinitys-floating-point-representation-apparently-0n