Why doesn't C++ support unsigned double syntax?
3 Answers
Because typical floating point formats don't support unsigned numbers. See, for instance, this list of IEEE 754 formats.
Adding a numerical format that isn't supported by common hardware just makes life difficult for compiler writers, and is probably not considered worth the effort.

- 391,730
- 64
- 469
- 606
-
2Also the *idea* is with floating point. That **Any** number should be representable, including the negative integers out to infinity. Unsigned numbers are counterproductive to this goal. – unixman83 Apr 20 '12 at 03:07
-
32Erm, no, that's not the point. Or if it is the point, then all floating point types are doomed to failure. The set of numbers that can ever be represented in a meaningful way in a computer is countable... – Brennan Vincent Apr 21 '12 at 22:01
C++ doesn't support unsigned floating point types because most floating point hardware doesn't support unsigned floating point types. Some graphics cards do work with unsigned floating point, but it's generally internal, not really visible to a program or user.

- 476,176
- 80
- 629
- 1,111
Unsigned integers gain an extra bit of precision and have slightly different bit-wise semantics to signed integers. Floats and doubles always reserve a bit for the sign (on most hardware) and have no bit-wise semantics, so there's no real benefit in having an unsigned real type.

- 181,030
- 38
- 327
- 365
-
13You theoretically *could* use that bit for the mantissa or exponent, though, increasing either range or accuracy. – Joey Mar 26 '10 at 12:11
-
2Well I wanted a unsigned double to control that it's always a positive number without continuing to check it. So yes there is benefit. – Daniel Ryan May 01 '13 at 22:20
-
@Јοеу: (Sorry for rather the late response.) Yes, it would help, but the question was about the lack of unsigned doubles in C++. It wasn't about why hardware doesn't support it. Adding the concept to C++ without adding support for it in hardware would gain you nothing. – Marcelo Cantos May 02 '13 at 09:58
-
@Zammbi: As per my response to Joey, adding such a thing to C++ without the hardware following suit would gain you nothing. You wouldn't get a signalling underflow, so you would have to perform such checks in code. This would incur an unacceptable performance hit, no matter whether you or the compiler did so. – Marcelo Cantos May 02 '13 at 10:02
-
3Of course there is a _reason_ for an unsigned double type - better type safety. – Benjamin Gruenbaum Jan 17 '16 at 10:47
-
1@BenjaminGruenbaum You could write `unsigned float x = -3;` and have the compiler complain. But if you subtract a larger number from a smaller number, most hardware won't catch the overflow any more than it does for unsigned int. So, yes, I'll concede that there might be a benefit, but it would be very marginal. – Marcelo Cantos Jan 17 '16 at 10:59