News for hackers. Stuff that matters.
Re:Almost Always Unsigned by joppy on Sunday January 02, 2022 @05:52AM (#29767664)
If my intent is that a certain integer should never be negative, labelling it unsigned does a poor job at conveying this intent because any arithmetic just silently does the wrong thing. 3-4 is some huge number rather than “error”.
Re:Almost Always Unsigned by tromp on Sunday January 02, 2022 @06:57AM (#29768015)
In Rust, 3u64 - 4u64 is "error", specifically

thread 'main' panicked at 'attempt to subtract with overflow'

Re:Almost Always Unsigned by Macha on Sunday January 02, 2022 @07:07AM (#29768073)
It panics _in debug builds_ but wraps in release builds because including the checks in every arithmetic operation is not zero cost.
Re:Almost Always Unsigned by masklinn on Sunday January 02, 2022 @08:39AM (#29768614)
Though you can configure your release builds to panic as well. That can be quite useful if you don't expect trivial computations to be a large cost center (and you can always bench to check that assumption).

Even more so now that custom profiles have been added to Cargo (yay), you can enable overflow checking in release, and add a `release-unchecked` or whatever.

Re:Almost Always Unsigned by tialaramex on Sunday January 02, 2022 @10:05AM (#29769240)
Or, better in many cases, you can write what you meant explicitly in Rust.

3u64.checked_sub(4u64).expect("Overflow");

This allows you to spell out that you don't expect this to overflow, and Rust should panic if it does, regardless of your compiler flags.

Re:Almost Always Unsigned by masklinn on Sunday January 02, 2022 @10:09AM (#29769272)
Of course you can do that, but I would say that doing so defensively for every operation is way more verbose and unwieldy than you'd want.

Much simpler to just toggle on the relevant flag.

Re:Almost Always Unsigned by eyelidlessness on Sunday January 02, 2022 @11:39AM (#29770201)
I think GP was saying you can do that when you expect that the general case doesn’t need to be checked but a specific case does.
Re:Almost Always Unsigned by masklinn on Sunday January 02, 2022 @11:54AM (#29770387)
> when you expect that the general case doesn’t need to be checked but a specific case does.

But surely that's almost never the case? Few programs actually desire or care for base 2 modular arithmetics, and when they do it tends to be for very specific tasks (usually cryptographic or cryptography-adjacent e.g. hashing, checksumming, ...).

Re:Almost Always Unsigned by eyelidlessness on Sunday January 02, 2022 @01:12PM (#29771637)
> But surely that's almost never the case?

Which is why it’s not so impractical to make an occasional exception.

Re:Almost Always Unsigned by deepsun on Monday January 03, 2022 @03:45AM (#29778645)
I wish they had "unchecked_sub()" instead, and default "-" would work like checked_sub().
Re:Almost Always Unsigned by tialaramex on Monday January 03, 2022 @05:01AM (#29779092)
unchecked_sub() exists in Nightly and so may some day come to stable

However, changing - to perform checked_sub() leaves you with an Option, and this makes ordinary arithmetic look pretty clumsy:

let x = (a-b).unwrap()-c;

The intent is to land more types with intrinsic behaviour. Today only the Wrapping type is provided, so Wrapping<i32> is an i32 that definitely has Wrapping arithmetic and won't panic on overflow, but eventually Saturating<i16> will be possible (e.g. for CD audio PCM samples, saturating arithmetic is correct, if you try to make the loudest possible noise louder it just stays the same) and so will Unchecked<i8> if you really sure that you're doing 8-bit arithmetic that can't overflow and doesn't need Debug checks.

Re:Almost Always Unsigned by raverbashing on Sunday January 02, 2022 @08:27AM (#29768527)
But then your function should check if the value is within reasonable parameters.

If it was an int at the function signature and you got -1 what would you expect to happen? It's the same thing.

Re:Almost Always Unsigned by marginalia_nu on Sunday January 02, 2022 @09:03AM (#29768757)
> But then your function should check if the value is within reasonable parameters

How could you possibly check this using types that cannot represent what you are checking for?

Re:Almost Always Unsigned by chromatin on Sunday January 02, 2022 @09:29AM (#29768928)
> How could you possibly check this using types that cannot represent what you are checking for?

`f(u32 a, u32 b) { assert a > b ... `

(if the hypothetical following operation is `a-b` as discussed higher in the thread)

Re:Almost Always Unsigned by pjmlp on Sunday January 02, 2022 @05:24PM (#29774700)
Better ensure that assert is not turned off in release mode then.
Re:Almost Always Unsigned by raverbashing on Monday January 03, 2022 @12:01AM (#29777508)
One of the reasons I don't like asserts.

Log the error and act accordingly, instead of believing this kind of issue only happens during development

Re:Almost Always Unsigned by jstimpfle on Monday January 03, 2022 @07:09AM (#29779948)
How do you "act" if you detected your logic is faulty? In many situations aborting is the best choice.

If input data is faulty and the reason for that is not seen as part of your logic, then sure, log an error and skip.

I used to define my own assert macro to "ensure" my asserts aren't disabled, but I don't bother anymore. There's nothing wrong with assert, and you needn't define NDEBUG. It's important to be aware that there are different situations, and not all warrant aborting, as described above. Another differentation is that there can be asserts that must be disabled in release builds for performance reasons, and others that won't affect performance and can stay enabled.

Re:Almost Always Unsigned by cesarb on Sunday January 02, 2022 @12:27PM (#29770903)
> because any arithmetic just silently does the wrong thing

The same thing, but worse, can happen with signed integers. (-3)-INT_MAX is either some huge positive number, or something crazy because it's undefined behavior and the compiler is allowed to do anything it wants.

Re:Almost Always Unsigned by dureuill on Sunday January 02, 2022 @06:13PM (#29775162)
I guess what the parent is saying is that, while the same kind of bugs can happen with signed integers, they are less common than with unsigned integers because they typically involve very big or very small values such as INT_MAX or INT_MIN. This is due to the fact that the "most common values" (close to 0) are at the beginning of the range of unsigned integers, but at the middle of the range of signed integers, so the risk of underflow is lower for signed integers in practice.

I can understand where they come from, especially regarding error handling: for integers whose value must not be negative, it is easier to check for underflow by checking if the result is not negative rather than by checking that the result is not eg smaller than the previous value (eg for addition).

That being said, "number must not be negative" really ought to be encoded in the type system, so that the user of the type knows that they must actually look for underflow.

I guess that part of the problem is that we don't want to check for underflow after each operation, so checking the negativity is a way to "coalesce" several checks after multiple operations. However this is fragile, because the multiple operations could end up producing a positive value, even if some intermediate values where negative.

For full safety, I don't see how we could do better than checking after each operation right now. If we're doing this, I feel like checked_add and friends from rust is a better fit than cramming an unsigned int into a signed one.

I wonder if we could design an integer type with 63 bits of value, plus one bit of "overflow/underflow poison", such that any operation that would under/overflow would saturate that bit to one, but otherwise still perform the operation on the value part. That would allow to coalesce multiple checks while keeping safety even in the presence of multiple faulty operations. I wonder how it could be implemented efficiently though

Re:Almost Always Unsigned by LegionMammal978 on Monday January 03, 2022 @01:22AM (#29777929)
Your idea for a coalescing overflow type has been explored in the document N2868 [0], a proposal for the C programming language. Instead of reserving a bit from the integer, each integer type is associated with a checked integer type, which holds an integer alongside an overflow bit. Checked integers are opaque and can only be constructed and deconstructed via macros; as I understand it, the idea is that the compiler can use the processor's preexisting overflow flag to represent the checked integer. In particular, the operations on checked integers have the property of coalescing overflow checks that you are looking for.

[0] Supplemental Integer Safety, http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2868.pdf

Re:Almost Always Unsigned by cesarb on Sunday January 02, 2022 @07:58PM (#29776051)
> I wonder if we could design an integer type with 63 bits of value, plus one bit of "overflow/underflow poison", such that any operation that would under/overflow would saturate that bit to one, but otherwise still perform the operation on the value part. That would allow to coalesce multiple checks while keeping safety even in the presence of multiple faulty operations. I wonder how it could be implemented efficiently though

There is something like that already for floating point. Whenever an overflow or underflow happens, it sets a sticky bit in a separate flags register. You can clear these flags, do a sequence of operations, and at the end, see if any of these flags are set. See https://man7.org/linux/man-pages/man3/fenv.3.html for the standard C API for it.

Re:Almost Always Unsigned by cle on Sunday January 02, 2022 @10:33AM (#29769483)
An unsigned integer will never be negative, that intent is expressed clearly and correctly. What the operational semantics should be is unclear, because of the tradeoffs involved in signaling an error.

In terms of the C standard, "3-4" for unsigned ints is modular arithmetic, and the "wrong thing" is assuming that it will do anything other than wrap around. This is very clearly defined, and implied whenever you see an arithmetic expression on unsigned integers.

Re:Almost Always Unsigned by kazinator on Sunday January 02, 2022 @11:26AM (#29770060)
Conversion from signed to unsigned is implicit and silent. The expression being converted to your "can never be negative" type isn't nonnegative!!! Its value drastically changes; e.g. -3 becomes a huge number.
Re:Almost Always Unsigned by snovv_crash on Sunday January 02, 2022 @04:38PM (#29774212)
Not if you pass -Wconversion to GCC
Re:Almost Always Unsigned by dagss on Sunday January 02, 2022 @11:48AM (#29770294)
Clearly defined, but very inconvenient and makes it too easy to write buggy code that looks correct on surface.
Re:Almost Always Unsigned by cle on Sunday January 02, 2022 @12:18PM (#29770744)
Inconvenient to whom? It’s pretty inconvenient not to do that if you eg have strict memory/performance requirements.

C made the right tradeoff IMO. You can protect yourself against overflow if you need to, but if it always signals errors you can’t turn that off.

Re:Almost Always Unsigned by willis936 on Sunday January 02, 2022 @08:10AM (#29768427)
This sounds like an issue with the language. 3-4 is not a large number and should not be by default. It should be a case handled by the developer.

Maybe you want to do some weird bit banging trickery, and that's fine too, but it should require an out-of-the-way function call.

Re:Almost Always Unsigned by dragonwriter on Sunday January 02, 2022 @01:21PM (#29771759)
> This sounds like an issue with the language. 3-4 is not a large number and should not be by default

Many languages optimize numeric operations and DX around numeric types for efficiency rather than correctness, and make the latter high-friction. (Wrap-around subtraction, decimal literals being treated as IEEE floats, etc.)

The exceptions (or cases where it is merely less true) tend to be very high level, dynamic languages.