modulus and array.length

2010-11-16 Thread David Osborne
Hi, I've noticed that the modulus operator acts differently when the divisor is the length of an array and the dividend is negative. For instance, this code: import std.stdio; const int x = 4; void main(){ int[x] arr = [0, 1, 2, 3]; writeln(Using arr.length); for(int i = -3; i = 0;

Re: modulus and array.length

2010-11-16 Thread Jens Mueller
Hi, I've noticed that the modulus operator acts differently when the divisor is the length of an array and the dividend is negative. For instance, this code: import std.stdio; const int x = 4; void main(){ int[x] arr = [0, 1, 2, 3]; writeln(Using arr.length); for(int i =

Re: modulus and array.length

2010-11-16 Thread Steven Schveighoffer
On Tue, 16 Nov 2010 10:55:24 -0500, David Osborne krendilbo...@gmail.com wrote: Hi, I've noticed that the modulus operator acts differently when the divisor is the length of an array and the dividend is negative. For instance, this code: import std.stdio; const int x = 4; void main(){

Re: modulus and array.length

2010-11-16 Thread Jérôme M. Berger
David Osborne wrote: Using arr.length -3 mod 3 = 1 -- this should be 0 -2 mod 3 = 2 -- this should be 1 -1 mod 3 = 0 -- this should be 2 0 mod 3 = 0 Like others have said, this is due to the fact that when one operand is unsigned (here the length), then all operands get casted to

Re: modulus and array.length

2010-11-16 Thread Kagamin
Jérôme M. Berger Wrote: -3 mod 3 = 0 -2 mod 3 = -2 -1 mod 3 = -1 0 mod 3 = 0 Note that from a strictly mathematical point of view, this result is valid: for all x and all n, x-(x%n) is a multiple of n. It's rather (x/n)+(x%n)==x

Re: modulus and array.length

2010-11-16 Thread Jérôme M. Berger
Kagamin wrote: Jérôme M. Berger Wrote: -3 mod 3 = 0 -2 mod 3 = -2 -1 mod 3 = -1 0 mod 3 = 0 Note that from a strictly mathematical point of view, this result is valid: for all x and all n, x-(x%n) is a multiple of n. It's rather (x/n)+(x%n)==x That is (part of) the

Re: modulus and array.length

2010-11-16 Thread bearophile
J. M. Berger: (bearophile probably has a bug report for it ;) ) Yup, bug 3843. Bye, bearophile

Re: modulus and array.length

2010-11-16 Thread David Osborne
Thanks for the info, everyone. I guess I'll just have to be more careful what I use modulus with :) ~Dave