Re:Fast midpoint between two integers without overflow
by tromp on Wednesday December 07, 2022 @08:55AM
(#33894337)
if a > b {
mid = (a-b)/2
That only avoids overflow on unsigned types (where it would be called wraparound) ...
In Rust you can actually do this using the checked add/sub/etc methods. It's one of the things I really appreciate about the language. By default, it panics on overflow in debug builds. You have to use the wrapping variants to declare that's intentional.
How about:
There must be a way to do it without the branch? edit: Yes, in the article, I'm an idiot.