Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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)



Or 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.


Not always, but overflows are usually bugs. This is replicated finding of dynamic overflow checking tools, over and over.


And 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: