I know, the question seems to be strange. Programmers sometimes think too much. Please read on...
In C I use signed
and unsigned
integers a lot. I like the fact that the compiler warns me if I do things like assigning a signed integer to an unsigned variable. I get warnings if I compare signed with unsigned integers and much much more.
I like these warnings. They help me to keep my code correct.
Why don't we have the same luxury for floats? A square-root will definitely never return a negative number. There are other places as well where a negative float value has no meaning. Perfect candidate for an unsigned float.
Btw - I'm not really keen about the single extra bit of precision that I could get by removing the sign bit from the floats. I'm super happy with float
s as they are right now. I'd just like to mark a float as unsigned sometimes and get the same kind of warnings that I get with integers.
I'm not aware of any programming language that supports unsigned floating-point numbers.
Any idea why they don't exist?
EDIT:
I know that the x87 FPU has no instructions to deal with unsigned floats. Lets just use the signed float instructions. Misuse (e.g. going below zero) could be considered undefined behaviour in the same way as overflow of signed integers is undefined.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…