News for hackers. Stuff that matters.
Re:Almost Always Unsigned by whattheactualf3 on Sunday January 02, 2022 @07:24AM (#29768169)
It does not express intent. I remember being a junior C programmer and using "unsigned" to express my intent (it's greater than or equal to zero).

In reality, like all C keywords, it's completely misleading and actually really means "weird_arithmetic_mode_that_will_mess_you_up".

Re:Almost Always Unsigned by carlmr on Sunday January 02, 2022 @08:19AM (#29768471)
I don't know what's misleading about it or how the arithmetic is weird?
Re:Almost Always Unsigned by SAI_Peregrinus on Sunday January 02, 2022 @01:38PM (#29771986)
Signed and unsigned integer types both are finite fields, rather than Abelian rings like the normal integers used in most math. That difference makes the arithmetic "weird", but going to signed integers doesn't change anything, anything except bigints (or generic arbitrary precision floating point) is a form of "weird_arithmetic_mode_that_will_mess_you_up" if you pretend you're operating over the real numbers.

Not sure what they mean about it being misleading though. They might prefer it if they were called "modular integers" instead of "unsigned integers" but that would lead to confusion with modules IMO.

Re:Almost Always Unsigned by carlmr on Monday January 10, 2022 @04:24AM (#29872287)
>Not sure what they mean about it being misleading though. They might prefer it if they were called "modular integers" instead of "unsigned integers" but that would lead to confusion with modules IMO.

Modular integers to me would seem very confusing. They're integers without the sign, and they have some max value determined by the number of bits, I can't really think of a better name for them except for the fixed-width versions which include that bit information.

Extremely intuitive, simple English words, exactly what it says it is (integer without the sign bit).

Re:Almost Always Unsigned by b3morales on Sunday January 02, 2022 @03:00PM (#29773085)
Imagine a parallel situation. There is a type `NonEmptySet`, with an `intersection` operation. Now, given `NonEmptySet(1, 2).intersection(NonEmptySet(3, 4))`, and the requirement that `intersection` produces another `NonEmptySet` what does that return? How about a set containing everything except 1, 2, 3, 4? Does that seem reasonable? That's analagous to what `(unsigned)3 - (unsigned)4` does.

In short, the operations on `unsigned int` do not really behave like "non-negative integer"; as such it's a weird match to use them for that purpose.

Re:Almost Always Unsigned by carlmr on Monday January 10, 2022 @04:31AM (#29872350)
I don't really see it, unsigned says there's no sign. Given that and the number of bits it's exactly the expected behavior that it will not be able to produce a sensible result for subtracting something larger from something smaller.

Whether the operation then errors out or wraps doesn't change anything about unsigned being the right name for it, because it's something you wouldn't expect unsigned to do.

The intersection of your two NonEmptySet produces an empty set. If you write that result into a NonEmptySet it's the same, it doesn't make sense for you to expect a NonEmptySet as a result of that operation.