Integer overflow and underflow semantics

renoX renozyx at gmail.com
Wed May 9 06:03:34 PDT 2012


On Monday, 7 May 2012 at 08:06:01 UTC, Don Clugston wrote:
> I think the reason that C makes no guarantees, was because of 
> ones-complement machines

Maybe but that's not the full reason, as Mehrdad says the 
compiler can use the fact that overflow are undefined to optimise 
the code generated.

In my opinion, there are only two valid options for the integer 
operation's overflow by default: either the compiler should make 
sure that the overflow is noticed (Ada: exception, adding 
'Not-a-Number' values would work too) or C's signed undefined 
behaviour.
Modulo operation (except where really useful) are bad: slower 
than the 'undefined' semantic, yet it still produce obviously 
incorrect behaviour such as negative absolute value..




More information about the Digitalmars-d mailing list