digitalmars.D.learn - [D1] modulo on neg integer
- qwerty (6/6) Mar 18 2010 If memory serves me right, module is undefined on negative integers.
- bearophile (6/12) Mar 18 2010 D outputs -2, Python outputs 8. It's not easy to find a language that ou...
- qwerty (9/25) Mar 18 2010 8 yeah :(somehow I did overcompensated for the max being 9 for %10)
- qwerty (3/10) Mar 18 2010 Yeah, here
- Don (9/16) Mar 18 2010 It's not undefined. x%y always has the sign of x, so i will be -2.
- qwerty (6/29) Mar 18 2010 Ok, that seems sensible.
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:Got url ? :)If memory serves me right, module is undefined on negative integers.I think it's defined in D (but it's defined badly).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;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?I'd wish it was available here, as I don't really know C(++).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:Yeah, here http://www.digitalmars.com/d/1.0/expression.html#MulExpressionqwerty:Got url ? :)If memory serves me right, module is undefined on negative integers.I think it's defined in D (but it's defined badly).
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:Ok, that seems sensible. 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 :)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.Yeah :(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