Hi. I am new to C programming and am working through some simple bits of code. Sometimes I come across a line like:
flag = (i%primes[j]);
In this case, the variable 'i', 'j' and 'flag' are integers and 'primes' is an array. I understand that '%' is used as a signifier in printf statements, e.g. where %d indicates a position for a decimal, but what does it mean in the above context?
Thanks
16 107147
Hi. I am new to C programming and am working through some simple bits of code. Sometimes I come across a line like:
flag = (i%primes[j]);
In this case, the variable 'i', 'j' and 'flag' are integers and 'primes' is an array. I understand that '%' is used as a signifier in printf statements, e.g. where %d indicates a position for a decimal, but what does it mean in the above context?
Thanks
it is used of mod operator .
it is used of mod operator .
The modulo operator returns the remainder of the left side divided by the right side. So 5%2 will evalutate to 1. (5 / 2 = 2 remainder : 1).
The modulo operator returns the remainder of the left side divided by the right side. So 5%2 will evalutate to 1. (5 / 2 = 2 remainder : 1).
Hi,
I just wanted to add % operator can be used only with "integers".
Regards,
Pradeep
Hi,
I just wanted to add % operator can be used only with "integers".
Regards,
Pradeep
And it deosn't make sense if divisor is 1,but I think we all know that.
:)
Savage
Thanks everyone. I thought it would be something simple!
And it deosn't make sense if divisor is 1,but I think we all know that.
:)
Savage
It makes perfect sense, just not very useful. ;)
I had thought that using a negitive as one of the paramters would give you a result that you might not expect, but I've just tested it and cannot determine what it was. Hmm.
Adrian
It makes perfect sense, just not very useful. ;)
I had thought that using a negitive as one of the paramters would give you a result that you might not expect, but I've just tested it and cannot determine what it was. Hmm.
Adrian
I don't know how (e.g)5%1 can make perfect sense to u.
Savage
I don't know how (e.g)5%1 can make perfect sense to u.
Savage
5/1 = 5 R 0
so 5%1 = 0.
Actually n%1 = 0. Like I said, not very useful.
Adrian
I had thought that using a negitive as one of the paramters would give you a result that you might not expect, but I've just tested it and cannot determine what it was. Hmm
I've heard the negative modulus is badly implemented in some languages (not sure with the c/c++), although I'm not sure whether this is true it gives unexpected results because people don't really understand negative mod (and expect the wrong result).....
The main confusion stems from what should a negative modulus return?
Assuming negative numbers are fins in modular arithmetic
what is the remainder of 7/3?, most people would say 1, but it is in fact 2 (or 1, if you prefer it that way)
Essentially, modulus is an equivalence operator, that looks for "the equivalent of this number between zeero and n", thus for the 7%3 example above: 
let x= 7

while(x not between 0 and 3)

x=x+3;

endwhile

print x

and x = 2;
if we change this to 7%3, is the result the same?
Actually, no. we are looking for "the equivalent of this number between zero and n", that is, we wnat 7's equivalent between 0 and 3. To find this we must reduce 7 into the range, so subtracting 3 three times, we get 2.
To cut a long story short, the % operator gives unexpected results if you expect the wrong result ;)
I've heard the negative modulus is badly implemented in some languages (not sure with the c/c++), although I'm not sure whether this is true it gives unexpected results because people don't really understand negative mod (and expect the wrong result).....
The main confusion stems from what should a negative modulus return?
Assuming negative numbers are fins in modular arithmetic
what is the remainder of 7/3?, most people would say 1, but it is in fact 2 (or 1, if you prefer it that way)
Essentially, modulus is an equivalence operator, that looks for "the equivalent of this number between zeero and n", thus for the 7%3 example above: 
let x= 7

while(x not between 0 and 3)

x=x+3;

endwhile

print x

and x = 2;
if we change this to 7%3, is the result the same?
Actually, no. we are looking for "the equivalent of this number between zero and n", that is, we wnat 7's equivalent between 0 and 3. To find this we must reduce 7 into the range, so subtracting 3 three times, we get 2.
To cut a long story short, the % operator gives unexpected results if you expect the wrong result ;)
That might have been it, it doesn't look strange to me now, but it may have before. ;)
Adrian
Or I could be wrong, we'll find out soon enough
Or I could be wrong, we'll find out soon enough
While looking up the C99 standard, I found this.
Point 25 is probably what was confusing me and causing me grief as I was getting 22 % 7 == 6 instead of 1. Both are correct, but it confused me way back, though I'm not sure if I realised back then it was inconsistent between compilers.
Adrian
I stand corrected (at least in computing terms)...
Mathematically I think it makes more sense to insiast that the number is trubncated to between 0 and the divisor (that is negative if the divisor is negative, but positive if the divisor is positive).....And that the numerator is irrelevant interms of the sign of the modulus....
I can understand, though, that it is easier to define in terms of whether you shorten it toward 0 or allow shortening to plus/minus infinity as well.....
The implementation of the modulo operator to return something like this:
22 % 7 = 6
makes all the same sense as why program language (except MATLAB) often use zero as the first index in an array.
In my opinion, the modulo operation has more applications to indexing than it does the mathematical sense of a modulus. This is because in C/C++ you are more likely to use modulo for pointers and counters.
Thus, the negative interpretation of a modulo is related to circular indexing. Take for example, you want to turn an array of data into a periodic array of data. This can be done identically by saying that data i=[0,6] goes from index [6,13] would produce three repetitions of the data as would [0,20]. When using the modulo operator, 6 % 6 should wrap to zero and that the next element in the array should be 5 % 6 = 1.
Hello everyone,
anyone could please explain me what the below operation does?
mj %= MBIG;
Regards
These expressions:
are equivalent to these expressions:  mj = mj % MBIG;

aj = aj + ABIG;
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
2 posts
views
Thread by Steve Richter 
last post: by

2 posts
views
Thread by Don 
last post: by

3 posts
views
Thread by RobertTG 
last post: by

6 posts
views
Thread by allenj 
last post: by

58 posts
views
Thread by Larry David 
last post: by

92 posts
views
Thread by Heinrich Pumpernickel 
last post: by

45 posts
views
Thread by loudking 
last post: by
          