Why no unsigned floating point types? [duplicate]
Possible Duplicate:
Why doesn't C have unsigned floats?
The question is probably very basic and probably answered many time earlier, but I want to understand why C++ does not have unsigned floating point types, even though floating point literals can be signed or unsigned.
开发者_开发知识库$3.9.1/8- "There are three floating point types: float, double, and long double."
Unsigned integer types have two important properties that differentiate them from signed integer types: "shifted" range (no negative subrange, but positive subrange twice as wide) and modulo arithmetic. For integer types these properties are important enough to justify the existence of unsigned types.
With floating-types neither of these properties are immediately applicable. With floating-point types the main issue is not in their range (for many purposes it can be thought of as virtually infinite), but rather in precision. And modulo arithmetic is not naturally applicable to non-integer types. For this reason, it didn't make much sense to introduce unsigned floating-point types, i.e. it didn't make much sense to flip-flop the role of just one bit in the floating-point representation.
It should also be noted that the above reasoning should probably be used as rationale behind introducing unsigned integer types (and not introducing unsigned floating-point types) in popular hardware and corresponding hardware-derived standards. What we have in C and C++ was essentially inherited from the hardware capabilities and these standards.
Of course, from the conceptual point if view, it would be quite logical to have unsigned floating-point types in the language, just for the sake of consistency. But, alas they are not there.
All floating points are signed. C++ follows the IEEE 754 standard, which is the most common hardware implementation and following it, floats are always signed.
As the floats already take up at least 32 bits, the gain of having a software implementation that would regain that 1 bit is insignificant compared to the usefullness of such an implementation.
精彩评论