-
The modulo operator
So I'm trying to understand the modulo operator and the use of it in a clock example.
The line:
Code:
public void increment()
{
value = (value + 1) % limit
}
Where value is defined a an integer is, if, I understand it correctly, meant to increment the "value" value with 1, untill reaching a preset limit at which the value will be set to 0, due to the fact that at this point there will be nothing remaing. i.e 24 % 24 = 0
However I thought when the result of the fraction were below zero as in for example 13 % 24 the remaing should be 13... which to me would screw the logic up in the given piece of code.
Well it doesn't and I therefore I must have misunderstood something.
Can anyone help me out there?
Thanks a lot
Martin
-
Re: The modulo operator
What's the remainder of 13 / 24? ... it's 13. So the logic holds.
-
Re: The modulo operator
Oh my god.... yes of course. I simply misread the code.
Thank you for pointing that out.
Cheers