Go Down

Topic: {Solved}Modulus (Read 1 time) previous topic - next topic

JB_AU

Oct 10, 2013, 08:52 am Last Edit: Oct 10, 2013, 10:01 am by JB_AU Reason: 1
I am still very vague as how modulus works in a program, i have read the reference & wiki, but it hasn't sunk in.

I have no code, but here is something ish.

Code: [Select]
int counter = 0;
int overflow = 0;


void setup(){ }

void loop()
{
if counter >= 32767 ?? // mod % 200, add the remainder back into counter, take 32600 divide by 200 & add to overflow
if counter <= -32767 // mod % 200, add the remainder back into counter, take -32600 divide by 200 & add to overflow

dosomething();
counter = counter++;

or

dosomethingelse();
counter = counter--;


That's roughly what i was thinking of doing, somehow?
Amateurs built the ark, Professionals built the Titanic

nickgammon

I don't really understand your question, but the modulus is just the remainder after a division.

eg.

Code: [Select]

11 % 6 --> 5


That is, if you divide 11 by 6 you get 1 with 5 remainder.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nid69ita

Also for me is not clear what do you want to do. Sorry.

Modulus give you remainder of division. Usually I used this operator to split the single numbers of a value.
Code: [Select]
int val=456;
int n1=val % 10;  // n1=6
val=val/10;
int n2=val%10; // n2=5
val=val/10;
int n3=val%10; // n3=4

Something like this. But is not the best code, you can use a loop like for or while.
my name is IGOR, not AIGOR

JB_AU

#3
Oct 10, 2013, 09:41 am Last Edit: Oct 10, 2013, 09:49 am by JB_AU Reason: 1
Quote
11 % 6 --> 5


How do you store the 1 & the remainder 5 ?

counter = 32767
remainder = counter % 200 // remainder = 167
overflow = (counter - remainder) / 200
counter = remainder

i think is right?
Amateurs built the ark, Professionals built the Titanic

nid69ita

you can use only a division:
overflow=counter/200;      

if every variable are integer like int or long you bring only integer part in overflow variable
or you can force using cast
overflow=(int)counter/200;      
my name is IGOR, not AIGOR

JB_AU

Thanks, that cleared it up for me  XD
Amateurs built the ark, Professionals built the Titanic

Go Up