5 ms·
That blog is wrong. The program won't be undefined( unsigned wrap is defined ), but the offset will not be correct.
by cautious_int 11y ago
That blog is wrong. The program won't be undefined( unsigned wrap is defined ), but the offset will not be correct.
- eloff 11y agoThey didn't say unsigned overflow was undefined, but you're right that the offset won't be correct. I thought it seemed intuitively wrong to me, but I figured google research is probably a reliable source!
- deleted 11y ago[deleted]
- banachtarski 11y agoThat's not true. Unsigned wrap is well-defined and wraps around to 0. Signed wrap is undefined.
- cautious_int 11y agoIt is not true that unsigned wrap is defined? Did you even read my comment, here is the relevant part: unsigned wrap is defined
- dastbe 11y agoSo the article isn't wrong, but its misleading you into thinking this should be correct for the wrong reasons. They are using signed integers as their indices, which means that the signed bit is always 0. Thus the addition after casting to unsigned will never overflow, and you can divide by two (shift by 1) and then recast to a signed integer, no harm no foul.
- cautious_int 11y agoActually it is misleading by them( and you ) to assume that in C, an unsigned int can represent values larger than the largest signed int value. In other words, C allows that UINT_MAX == INT_MAX, in which case you will overflow. If they made that assumption, they should explicitly mention it, but they didn't. > Update 17 Feb 2008:... ...Now that we've made this change, we know that the program is correct;) It seems the article is aware of the irony. Another update would be in order.
- deleted 11y ago[deleted]
- dastbe 11y agoSure, and if you expect your software to run on such a platform with any degree of confidence then you're right to consider it. Even better, tell us about a conforming implementation that you've used in product recently that has UINT_MAX == INT_MAX. Also I'm not trying to mislead people into thinking that its a good way to implement this. The confounding bit from the article is they started in Java and ended up in C. If you were indexing with signed ints in C, C++, or any language that has unsigned integers then you already have a bug with or without the bad mean check.
- cautious_int 11y agoYou got it backwards there. Only if you know your implementation and plan to code only for it, can you even start to consider bending the C Standard, and not the other way around.