21

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

Community
  • 1
  • 1
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
  • @Greg Hewgill: Yup, changed the title buddy. – Chubsdad Aug 28 '10 at 06:03
  • @Greg Hewgill: updated as per your comments. – Chubsdad Aug 28 '10 at 06:06
  • Floating point literals are always signed in terms of *type*. Whether or not you explicitly prefix with +/- does not change that and suffixing with U or LU as you would an integer literal to coerce the type is an error. – Clifford Aug 28 '10 at 06:17
  • @Thilo: Thanks for pointing out the thread. It did not come up in my googling. Yes, I think one of you can mark this as closed. – Chubsdad Aug 28 '10 at 06:18

2 Answers2

38

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 2
    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 Aug 28 '10 at 06:19
  • It's worthwhile to note that the behavior of subtracting a larger unsigned value from a smaller one is well-defined, but there's no clear meaning for subtracting a larger float from a smaller one, other than reporting a negative result. – supercat Jul 17 '13 at 19:32
10

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
  • 55,802
  • 15
  • 111
  • 149
  • 1
    I would omit "by default", since there isn't any alternative. – Greg Hewgill Aug 28 '10 at 06:08
  • 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 Aug 28 '10 at 06:08
  • @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 Aug 28 '10 at 06:12
  • 1
    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::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 Aug 28 '10 at 07:07