I realized I haven't seen this type in Objective C or Swift. But we have UInt.

Is there some more Computer Science specific reason we do not have an Unsigned Float? Afaik it's not in c# too.


Ultimately, the answer is that the concept of unsigned floating point numbers is not particularly useful. Concepts that are extremely useful make their way into the standards. Concepts that are marginally useful might make their way into add-on packages. The concept of an unsigned float doesn't even fit into the marginally useful concept. It throws out far too much of the mathematical concept of a field.

  • 2
    For example: You'd have to define the result of 2.0u - 3.0u (possibly as undefined behaviour). – gnasher729 Jul 24 '16 at 20:57
  • I doubt this reasoning. To me it seems more of a performance optimisation trumping compile-time verification. stackoverflow.com/questions/512022/… – Den Jul 28 '16 at 8:58

Unsigned ints are mostly a performance optimization desperately trying to save even one bit of memory.

Floats, on the other hand, weren't viable anyway for such systems, so there's less pressure on them to make such tiny savings. Their range is already huge anyway. The implementation of unsigned integers in modern languages is more about compatibility than usefulness.

  • There's also the fact that if you're going to break up a bigger integer type you need unsigned pieces to make it out of. Try to take apart an int64 and you need a uint32 type to do it with. Before x64 look at how many WinAPI calls returned two uint32s for something that really was a uint64. – Loren Pechtel Jul 24 '16 at 23:19
  • That's not quite true. You don't save a bit of memory by using an unsigned int instead of an signed int. It's still 32 bits. – RubberDuck Jul 25 '16 at 1:54
  • Actually the size of an int or unsigned int depends on your compiler and target platform. Sizes of 16, 32 and 64 bits are common. But unsigned does not save any space. It changes how the storage is interpreted. Even things like add and subtract are no different in terms of underlying instructions and their results (provided 2's complement signed is used). Where there is the difference is in type conversions where for example a conversion from 16 bit unsigned to 32 bit unsigned pads the most significant bits with 0; whereas doing the same for signed will sign extend (copy the most sig bit up). – quickly_now Jul 25 '16 at 3:44
  • You do indeed save one bit of memory by using an unsigned int. Although strictly speaking, the memory is still allocated to it, you can still use the same positive range but use that extra bit for something else if you mask it. It is of course simplest to use that extra bit by making use of the larger positive range. – DeadMG Jul 25 '16 at 16:00
  • Actually the reason for Uints is parsing or creating a compressed binary stream. This doesn't happen much any more but before XML nearly all meta data was compressed into some kind of bit field to save space and transmission time. Now you mostly see it on packet headers that get sent repeatedly. – Bill K Jul 25 '16 at 22:05

Integers serve multiple purposes. You can use them to calculate (add, subtract, multiply) but you can also use them to count or as identifiers (key values) or as memory locations (pointers). For the latter two, positive values will do and an unsigned version will conveniently double your range.

For floating point numbers there is only one application: calculations. And calculations without minus is meaningless. Plus (haha), range is not an issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.