www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - modulo Strangeness?

reply "Taylor Hillegeist" <taylorh140 gmail.com> writes:
I have a simpleish bit of code here that always seems to give me 
an error, and i can't figure out quite why. If I have a constant 
43 in the modulo if breaks. however if i use points.length it 
seems to be ok?

import std.stdio;
void main(){
	int points[43] = [0, 1153, 1905, 1996, 1392,  305, 
-888,-1773,-2041,-1600, -603,  603, 1600,2041, 1773,  888, 
-305,-1392,-1996,-1905,-1153, -0,1153, 1905, 1996, 1392,305, 
-888,-1773,-2041,-1600, -603,  603, 1600, 2041, 1773,  888, 
-305,-1392,-1996,-1905,-1153,    0];
	
	foreach(int x; points){
		writeln("List Value: ",points[(x%43)],"\t");
	}

}
Jun 11 2014
next sibling parent reply "Taylor Hillegeist" <taylorh140 gmail.com> writes:
On Wednesday, 11 June 2014 at 22:32:45 UTC, Taylor Hillegeist 
wrote:
 I have a simpleish bit of code here that always seems to give 
 me an error, and i can't figure out quite why. If I have a 
 constant 43 in the modulo if breaks. however if i use 
 points.length it seems to be ok?

 import std.stdio;
 void main(){
 	int points[43] = [0, 1153, 1905, 1996, 1392,  305, 
 -888,-1773,-2041,-1600, -603,  603, 1600,2041, 1773,  888, 
 -305,-1392,-1996,-1905,-1153, -0,1153, 1905, 1996, 1392,305, 
 -888,-1773,-2041,-1600, -603,  603, 1600, 2041, 1773,  888, 
 -305,-1392,-1996,-1905,-1153,    0];
 	
 	foreach(int x; points){
 		writeln("List Value: ",points[(x%43)],"\t");
 	}

 }
Perhaps i am stupid.... 0..points.length lol? i've been looking at this too long.
Jun 11 2014
parent reply "Taylor Hillegeist" <taylorh140 gmail.com> writes:
On Wednesday, 11 June 2014 at 22:35:39 UTC, Taylor Hillegeist 
wrote:
 On Wednesday, 11 June 2014 at 22:32:45 UTC, Taylor Hillegeist 
 wrote:
 I have a simpleish bit of code here that always seems to give 
 me an error, and i can't figure out quite why. If I have a 
 constant 43 in the modulo if breaks. however if i use 
 points.length it seems to be ok?

 import std.stdio;
 void main(){
 	int points[43] = [0, 1153, 1905, 1996, 1392,  305, 
 -888,-1773,-2041,-1600, -603,  603, 1600,2041, 1773,  888, 
 -305,-1392,-1996,-1905,-1153, -0,1153, 1905, 1996, 1392,305, 
 -888,-1773,-2041,-1600, -603,  603, 1600, 2041, 1773,  888, 
 -305,-1392,-1996,-1905,-1153,    0];
 	
 	foreach(int x; points){
 		writeln("List Value: ",points[(x%43)],"\t");
 	}

 }
Perhaps i am stupid.... 0..points.length lol? i've been looking at this too long.
foreach(uint x; points){ writeln("List Value: ",points[(x%43)],"\t"); } this must work because its a different type than an element in points[]
Jun 11 2014
parent "Taylor Hillegeist" <taylorh140 gmail.com> writes:
On Wednesday, 11 June 2014 at 22:38:02 UTC, Taylor Hillegeist 
wrote:
 On Wednesday, 11 June 2014 at 22:35:39 UTC, Taylor Hillegeist 
 wrote:
 On Wednesday, 11 June 2014 at 22:32:45 UTC, Taylor Hillegeist 
 wrote:
 I have a simpleish bit of code here that always seems to give 
 me an error, and i can't figure out quite why. If I have a 
 constant 43 in the modulo if breaks. however if i use 
 points.length it seems to be ok?

 import std.stdio;
 void main(){
 	int points[43] = [0, 1153, 1905, 1996, 1392,  305, 
 -888,-1773,-2041,-1600, -603,  603, 1600,2041, 1773,  888, 
 -305,-1392,-1996,-1905,-1153, -0,1153, 1905, 1996, 1392,305, 
 -888,-1773,-2041,-1600, -603,  603, 1600, 2041, 1773,  888, 
 -305,-1392,-1996,-1905,-1153,    0];
 	
 	foreach(int x; points){
 		writeln("List Value: ",points[(x%43)],"\t");
 	}

 }
Perhaps i am stupid.... 0..points.length lol? i've been looking at this too long.
foreach(uint x; points){ writeln("List Value: ",points[(x%43)],"\t"); } this must work because its a different type than an element in points[]
Negative indexes are possible with modulo... that is why i am getting breaking... sorry all.
Jun 11 2014
prev sibling next sibling parent "safety0ff" <safety0ff.dev gmail.com> writes:
On Wednesday, 11 June 2014 at 22:32:45 UTC, Taylor Hillegeist 
wrote:
 I have a simpleish bit of code here that always seems to give 
 me an error, and i can't figure out quite why.
modulo takes the sign of the dividend: http://en.wikipedia.org/wiki/Modulo_operation#Common_pitfalls It works with length because you introduce a signed -> unsigned conversion.
Jun 11 2014
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
modulo of a negative number can give some surprising results. A 
negative index in that array would cause it to throw a range 
error, so my guess is that's what you're getting. If you do 
%array.length though it becomes an unsigned math and thus will 
never be negative, explaining the different result.

Remember btw that when foreaching over an array, the value you 
get is the number in the array, not the index.
Jun 11 2014