News for hackers. Stuff that matters.
Re:Swedish Exchange paralyzed by buy order for "-6 stocks" by Jabbles on Thursday November 29, 2012 @06:08AM (#4847118)
That's quite a controversial statement. Using unsigned ints may just clutter code without providing any safety. The value of 4e9 is still invalid, even though it's positive. As you say, checking your inputs is important, and in this case values can be too large, and should be rejected.

For instance: http://google-styleguide.googlecode.com/svn/trunk/cppguide.x...

Re:Swedish Exchange paralyzed by buy order for "-6 stocks" by nanex on Thursday November 29, 2012 @06:32AM (#4847177)
Using "unsigned ints" for values that will never be negative allows you to perform one validation test instead of two. Use a typedef to avoid writing "unsigned" everywhere and you end up with less clutter in the code (for humans) and in the binary (for machines).

nanex

Re:Swedish Exchange paralyzed by buy order for "-6 stocks" by ajuc on Thursday November 29, 2012 @06:57AM (#4847245)
You should check before the operation that can produce result outside the valid range anyway.

Wrong example:

    unsigned int z = x - y;
    if (z > BIG_NUMBER) {
        return false;
    }
Good example:

    if (y > x) {
        return false;
    }
    unsigned int z = x - y;
Re:Swedish Exchange paralyzed by buy order for "-6 stocks" by raverbashing on Thursday November 29, 2012 @06:27AM (#4847163)
The issue is

4e9 is invalid but in the middle of the way (more often than not) this may be interpreted as a negative number.

Hence you need to check for value 'bigger than allowed' and 'smaller than allowed'

For unsigned ints, there is a natural smallest value allowed: zero.

You're free to not follow my advice though.

And the google style guide is subtle, it's not what you're implying. You can't use 'short' for example unless explicitly.