On 19 Oct 2014 09:40, "monarch_dodra via Digitalmars-d" < [email protected]> wrote: > > On Saturday, 18 October 2014 at 23:10:15 UTC, Ola Fosheim Grøstad wrote: >> >> On Saturday, 18 October 2014 at 08:22:25 UTC, monarch_dodra wrote: >>> >>> Besides, the code uses x + 1, so the code is already in undefined state. It's just as wrong as the "horrible code with UB" we wère trying to avoid in the first place. >>> >>> So much for convincing me that it's a good idea... >> >> >> Not sure if you are saying that modulo-arithmetic as a default is a bad or good idea? > > > Op usually suggested that all overflows should be undefined behavior, and that you could "pre-emptivelly" check for overflow with the above code. The code provided itself overflowed, so was also undefined. > > What I'm pointing out is that working with undefined behavior overflow is exceptionally difficult, see later. > > >> In D and (C++ for uint) it is modulo-arithmetic so it is defined as a circular type with at discontinuity which makes reasoning about integers harder. > > > What interesting is that overflow is only defined for unsigned integers. signed integer overflow is *undefined*, and GCC *will* optimize away any conditions that rely on it. >
Good thing that overflow is strictly defined in D then. You can rely on overflowing to occur rather than be optimised away. Iain.
