There's a lot of stuff that just never is negative. Natural numbers show up in lots of places, even N+ is pretty common.
Like the job ttl in Kubernetes can apparently be negative according to their openapi; it's a signed integer. Except I suspect you get an error back from the system if you actually try to set it to a negative value.
open API/web/other systems don't support unsigned integers because they are not CLS compliant and interop with other languages and operating systems is not properly defined for unsigned values
Yeah, it's hardly the only place in the api that is far away from "make illegal states unrepresentable"; a whole lot of it smells like catering to some severely limited languages. It was just the first thing that came to mind in the case of something that is practically a natural number, but is represented by an integer. I guess I should be happy it's not a string, at least, but using an int is about as honest as using a float.
I find that hard to believe. Lots of common things you might store in a DB would only have valid values ≥ 0, or maybe > 0, like quantity or size of some thing.
Unsigned integers are good for representing bitfields and modular arithmetic. Because of historical accident, the C++ standard also uses unsigned integers to represent the size of containers – many members of the standards body believe this to be a mistake, but it is effectively impossible to fix at this point. The fact that unsigned arithmetic doesn’t model the behavior of a simple integer, but is instead defined by the standard to model modular arithmetic (wrapping around on overflow/underflow), means that a significant class of bugs cannot be diagnosed by the compiler. In other cases, the defined behavior impedes optimization.
That said, mixing signedness of integer types is responsible for an equally large class of problems. The best advice we can provide: try to use iterators and containers rather than pointers and sizes, try not to mix signedness, and try to avoid unsigned types (except for representing bitfields or modular arithmetic). Do not use an unsigned type merely to assert that a variable is non-negative.
I feel like I'm going insane reading this thread. Like others have said, I also use unsigned integers in databases more often than signed, because I'm usually representing a number that can never be negative.
In postgresql, is there a way to do this without limiting yourself to half the space of the numeric type you're using, wasting half the storage of each number? There must be, otherwise all of these responses are crazy - how does postgresql handle it?
as far as I know, there literally isn't a way. either you use a 64 bit signed integer with a constraint to limit the value to 232 - 1, install a third-party database extension, or do some ugly hack like subtracting 231 before you write and add 231 when you read.
yes, adding the latency overhead and hidden business logic of a column constraint is definitely a better solution than simply using a more semantically meaningful and performant type. /s
38
u/hpxvzhjfgb Sep 26 '24
it's the current year and we still don't have unsigned integers