Thread overview |
---|
March 18, 2010 [D1] modulo on neg integer | ||||
---|---|---|---|---|
| ||||
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. |
March 18, 2010 Re: [D1] modulo on neg integer | ||||
---|---|---|---|---|
| ||||
Posted in reply to qwerty | 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 |
March 18, 2010 Re: [D1] modulo on neg integer | ||||
---|---|---|---|---|
| ||||
Posted in reply to qwerty | 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. |
March 18, 2010 Re: [D1] modulo on neg integer | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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). Got url ? :) > > > > 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. I'd wish it was available here, as I don't really know C(++). |
March 18, 2010 Re: [D1] modulo on neg integer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don | 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. 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 :) > > > What should I use instead to get i definitely equal to 7? > > You mean 8? Yeah :( > > 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. > |
March 18, 2010 Re: [D1] modulo on neg integer | ||||
---|---|---|---|---|
| ||||
Posted in reply to qwerty | 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). > Got url ? :) Yeah, here http://www.digitalmars.com/d/1.0/expression.html#MulExpression |
Copyright © 1999-2021 by the D Language Foundation