Modular arithmetic

I would expect any operation mod n to return a value in [ 0 , n - 1 ]. Using the % operator in Daz Script that works for addition but not for subtraction:

for ( var i = 0 ; i < 4 ; i++ ) {	print ( ( i + 1 ) % 4 );	print ( ( i - 1 ) % 4 );}

returns:

1
-1
2
0
3
1
0
2

instead of  the expected (by me):

1
3
2
0
3
1
0
2

Which of us is wrong, Daz Script or me? And if it's the latter, is there a neater way to get my expected behaviour than testing for 0 in subtraction operations and giving it a special result of n - 1 instead of -1?

Comments

Sign In or Register to comment.