digitalmars.D.learn - [D1] modulo on neg integer
- qwerty <qw er.ty> Mar 18 2010
- bearophile <bearophileHUGS lycos.com> Mar 18 2010
- qwerty <qw er.ty> Mar 18 2010
- qwerty <qw er.ty> Mar 18 2010
- Don <nospam nospam.com> Mar 18 2010
- qwerty <qw er.ty> Mar 18 2010
If memory serves me right, module is undefined on negative integers. int i = -2; i%=10; // i=undefined? What should I use instead to get i definitely equal to 7? On a sidenote, where can I read about operation order/definitions? Like i++, ++i and i%10 not changing i etc.
Mar 18 2010
qwerty:If memory serves me right, module is undefined on negative integers.
I think it's defined in D (but it's defined badly).int i = -2; i%=10; // i=undefined? What should I use instead to get i definitely equal to 7?
D outputs -2, Python outputs 8. It's not easy to find a language that outputs 7 there, maybe Malborge language?On a sidenote, where can I read about operation order/definitions? Like i++, ++i and i%10 not changing i etc.
D acts like C, here. Even when C does something badly. So you can surely find info about ++ and % in C. Bye, bearophile
Mar 18 2010
bearophile Wrote:qwerty:If memory serves me right, module is undefined on negative integers.
I think it's defined in D (but it's defined badly).
int i = -2; i%=10; // i=undefined? What should I use instead to get i definitely equal to 7?
D outputs -2, Python outputs 8. It's not easy to find a language that outputs 7 there, maybe Malborge language?
8 yeah :(somehow I did overcompensated for the max being 9 for %10) i = -3 -2 -1 0 1 2 i%3 = 7 8 9 0 1 2 So I should be safe with i % M; if(i<0) i = M + i;On a sidenote, where can I read about operation order/definitions? Like i++, ++i and i%10 not changing i etc.
D acts like C, here. Even when C does something badly. So you can surely find info about ++ and % in C.
Mar 18 2010
qwerty Wrote:bearophile Wrote:qwerty:If memory serves me right, module is undefined on negative integers.
I think it's defined in D (but it's defined badly).
http://www.digitalmars.com/d/1.0/expression.html#MulExpression
Mar 18 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.
Mar 18 2010
Don Wrote: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.
Yay,found the spec (mul expressions ?:): "For integral operands of the / and % operators, the quotient rounds towards zero and the remainder has the same sign as the dividend. If the divisor is zero, an Exception is thrown. " Personally I find your explanation a lot more clear :)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.
Mar 18 2010









qwerty <qw er.ty> 