Unsigneds in order to prevent negative numbers

Posted by Bruno Brant on Stack Overflow See other posts from Stack Overflow or by Bruno Brant
Published on 2010-05-06T18:52:45Z Indexed on 2010/05/06 19:18 UTC
Read the original article Hit count: 203

Filed under:
|

let's rope 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?

© Stack Overflow or respective owner

Related posts about language-agnostic

Related posts about unsigned