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;
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 =
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(){
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
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
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
J. M. Berger:
(bearophile probably has a bug report for it ;) )
Yup, bug 3843.
Bye,
bearophile
Thanks for the info, everyone. I guess I'll just have to be more careful
what I use modulus with :)
~Dave