Possible bug with % operator: can you repeat it?

I have to try this with 0018, I just realized I am on 0016. This is why I am not posting it in the bugs forum. I am not going to have access to my project for a couple of days. Does this problem occur with someone else?

I have to do this

ind = ((ind-1) % MAX_NUM);
if(ind < 0) { ind = MAX_NUM-1; }

It seems like mod isn’t working when I subtract values.

What type is ind? What value does MAX_NUM have?

What value of ind causes the problem?

I suspect that MOD just behaves differently than you’re expecting when it operates on -1 as the numerator. Or when it wraps around 0 in the unsigned case. Have you declared “ind” as “unsigned” ? I used this sketch and get:

void setup()
{
  Serial.begin(19200);
}

int i;
unsigned int u;
#define MAX_NUM 10

void loop()
{
  Serial.println("Signed modulous");
  for (int x=0; x < 3*MAX_NUM; x++) {
    i = (i-1) % MAX_NUM;
    Serial.print(i);    
    Serial.print(" ");
  }

  Serial.println(" ");
  Serial.println("UNSigned modulous");
  for (int x=0; x < 3*MAX_NUM; x++) {
    u = (u-1) % MAX_NUM;
    Serial.print(u);    
    Serial.print(" ");
  }
  Serial.println(" ");
  delay(10000L);
}
Signed modulous
-1 -2 -3 -4 -5 -6 -7 -8 -9 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 0  
UNSigned modulous
5 4 3 2 1 0 5 4 3 2 1 0 5 4 3 2 1 0 5 4 3 2 1 0 5 4 3 2 1 0

Both sets of numbers are a bit of a surprise, but I think they both make sense.
(0-1) == 65535 with an unsigned int, and 65535 mod 10 is certainly 5…

ind is an int. MAX_NUM is 10. When ind is 0 it will end up at -1 without that if.

Edit: ok, so signed behaves differently. Will switch to unsigned.