[D1] modulo on neg integer
Don
nospam at nospam.com
Thu Mar 18 05:28:24 PDT 2010
qwerty wrote:
> If memory serves me right, module is undefined on negative integers.
> int i = -2;
> i%=10; // i=undefined?
It's not undefined. x%y always has the sign of x, so i will be -2.
This always holds:
x == y * (x/y) + (x%y);
And since D uses truncated division, the result follows.
This behaviour is inherited from C99.
> What should I use instead to get i definitely equal to 7?
You mean 8?
i %= 10;
if (i<0) i += 10;
>
> On a sidenote, where can I read about operation order/definitions?
> Like i++, ++i and i%10 not changing i etc.
More information about the Digitalmars-d-learn
mailing list