5 ms·
> (negative number was cast to an unsigned huge number) Slightly off-topic pet-peeve of mine: never used unsigned, unless you have an extremely good reason for
by Iv 10y ago
> (negative number was cast to an unsigned huge number)
Slightly off-topic pet-peeve of mine: never used unsigned, unless you have an extremely good reason for it. Saving one bit is not one of them. You never know when you will need to do arithmetic on your unsigned values. Time going backwards is a good example of this.
- __s 10y agoThis crops up in Rust's std::time::Duration which has no concept of negative duration
- ioquatix 10y agoIf you think of time like a mathematical vector, the length of that vector can never be negative.. it's always >= 0.. sure, though, in 1D, encoding the direction of that vector as +ve or -ve is pretty convenient.
- cjhanks 10y agoI am the opposite way. In my experience usage of signed where the underlying type is conceptually unsigned introduces subtle bugs. Whereas the defined behavior of unsigned wrap usually causes very obvious stalls and overflows. I have seen so many array access of -1 which never SEGV until you upgrade your compiler or change platform. Also pointers are typically unsigned, I suspect same-type operations are faster for array access... though I never have checked.