views:

137

answers:

2

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."

+4  A: 

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.

Kornel Kisielewicz
I would omit "by default", since there isn't any alternative.
Greg Hewgill
@Kornel Kisielewicz: Basically I was looking at a way to avoid a double / float type variable from taking on negative value, by saying something like 'unsigned double'
Chubsdad
@Greg, corrected.
Kornel Kisielewicz
@chubsdad, well, even if they'd be unsigned, a FPE isn't exactly what I'd call "avoiding" :). BTW, there's a boost library that does that.
Kornel Kisielewicz
While most C++ implementations use IEEE-754 floating point representation, I don't think this is mandated by the C++ standard. Otherwise `std::numeric_limits<double>::is_iec559` could not evaluate to anything other than true in a conforming implementation. This would cause problems for architectures, like Cray and IBM mainframes, that use proprietary floating point representations. (Not that it makes a difference to chubsdad's question, though.)
Jim Lewis
+5  A: 

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 arithmetics. 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 the range (for most purposes it can be though of as virtually infinite), but rather in precision. And modulo arithmetics 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.

AndreyT
Accepting this answer for the last statement : "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." Also the reasoning on "important enough to justify the existence" seems right, though I am not an expert in C++
Chubsdad