let’s hope I can make this non-sujective
Here’s the thing: Sometimes, on fixed-typed languages, I restrict input on methods and functions to positive numbers by using the unsigned types, like unsigned int or unsigned double, etc.
Most libraries, however, doesn’t seem to think that way. Take C# string.Length. It’s a integer, even though it can never be negative. Same goes for C/C++: sqrt input is an int or a double. I know there are reasons for this … for example your argument might be read from a file and (no idea why) you may prefer to send the value directly to the function and check for errors latter (or use a try-catch block).
So, I’m assuming that libraries are way better designed than my own code. So what are the reasons against using unsigned numbers to represent positive numbers? It’s because of overflow when we cast then back to signed types?
In the case of C & C++, a lot of the libraries existed before
unsignedentered the language, so vanillaintwas all that was available. Newer additions to the libraries will use unsigned types such assize_t.In the case of .NET, there are languages (e.g. Visual Basic) that don’t have the concept of unsigned integers (at least as language features — they can of course use the System.UInt32, etc. types).