Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 843
Chubsdad Avatar asked Aug 28 '10 06:08

Chubsdad


People also ask

Why is there no unsigned float?

Not possible with floating point. Unsigned int is used to represent addresses and sizes, so computers natually need them. There isn't a need like this for floating point.

Can floating point types be unsigned?

The floating-point family has the same attributes and acts or behaves similarly in all programming languages. They can always store negative or positive values thus they always are signed; unlike the integer data type that could be unsigned.

Why is there no unsigned double?

You can not have unsigned floating point types. Therefore no unsigned double or unsigned float. unsigned can only be applied to integer types like char short int and long. The reason we have unsigned integers is that they can store numbers twice as large, and can be better suited for handling binary data.

Are floats always signed?

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.


2 Answers

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.

like image 181
AnT Avatar answered Sep 21 '22 16:09

AnT


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.

like image 45
Kornel Kisielewicz Avatar answered Sep 21 '22 16:09

Kornel Kisielewicz