Signed integer overflow undefined behavior or not?

Matthias Bentrup via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 13 04:06:40 PST 2015


On Friday, 13 November 2015 at 09:33:51 UTC, John Colvin wrote:
> unsigned: f(v) = v mod 2^n - 1
> signed: f(v) = ((v + 2^(n-1)) mod (2^n - 1)) - 2^(n-1)

I guess you meant mod 2^n in both cases...

If you look at how Mathematics deals with this issue, there is 
simply no signed or unsigned arithmetic modulo n, because they 
are exactly the same. There are only separate types in 
programming languages because the comparison operators are 
defined differently on them.

Mathematicians don't define comparison on modular rings, because 
it is not possible to do so in a way that is consistent with the 
usual rules anyway (e.g. x+1 > x is always false for some x).



More information about the Digitalmars-d mailing list