modulo Strangeness?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 11 15:41:29 PDT 2014


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.


More information about the Digitalmars-d-learn mailing list