Go Down

Topic: modulo with negative int (Read 941 times) previous topic - next topic


I'm having some difficulties in cycling between a range of value..

Code: [Select]

int c = 0;
int length = 4;
c = -1 % length;

If I use this code I get -1 as result..if I use "unsigned" for c, I get 65535.
I'd like to get 3: (https://www.google.it/#output=search&sclient=psy-ab&q=-1+modulo+4&oq=-1+modulo+4&gs_l=hp.3...1052.1052.0.1909.,or.r_qf.&bvm=bv.45512109,d.ZWU&fp=eb10b05648b700fa&biw=1440&bih=755)

If I'm not wrong I used to use % to get always a number in the range of the divisor, both when using negative or positive dividend..
what I'm missing?


Apr 22, 2013, 12:12 pm Last Edit: Apr 22, 2013, 12:25 pm by AWOL Reason: 1

Intro paragraph gives you the answer.

Moderator edit: Tags removed
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131