5 ms·
Yes, it is annoying to rrad comments that assume overflow is always a programming error. You can store the angle as a union of signed and unsigned type. Do ari
by markrages 8y ago
Yes, it is annoying to rrad comments that assume overflow is always a programming error.
You can store the angle as a union of signed and unsigned type. Do arithmetic on the unsigned member, where overflow is defined. (Both members are equivalent angles)
- sanxiyn 8y agoNot always, but overflows are usually bugs. This is replicated finding of dynamic overflow checking tools, over and over.
- kazinator 8y agoAnd if overflow is well-defined, then those tools must switch your C dialect to a non-ISO-C conforming one in order to do their job.
- kazinator 8y agoOr you could just convert from the unsigned to the signed type, rather than dragging unions into it. This conversion doesn't have undefined behavior; it produces an implementation defined result. C programs can simulate two's complement math using unsigned types, avoiding UB. Then rely on IB to convert between signed and unsigned.