Thread: Help understanding the modulus operator

  1. #1
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67

    Help understanding the modulus operator

    Hello,

    I'm having difficulty understanding how the modulus operator works. For instance, I have a piece of code:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i;
    	
    	for (i=0; i<8; i++)
    			printf("index_1[%d] = %d\n", i, (i % 2));
    	
    	for (i=0; i<8; i++)
    			printf("index_2[%d] = %d\n", i, (i % 8));	
    
    	return 0;
    }
    I understand how I come to the remainder of 1 for the results listed for index_1:

    index_1[0] = 0
    index_1[1] = 1
    index_1[2] = 0
    index_1[3] = 1
    index_1[4] = 0
    index_1[5] = 1
    index_1[6] = 0
    index_1[7] = 1

    But I don't understand how I am coming to the results listed for index_2:

    index_2[0] = 0
    index_2[1] = 1
    index_2[2] = 2
    index_2[3] = 3
    index_2[4] = 4
    index_2[5] = 5
    index_2[6] = 6
    index_2[7] = 7

    For example, 4 % 8 doesn't have a remainder because the result of the division is 0.5. Likewise for 1 % 8, the result of the division is 0.125. How is it arriving at these numbers? Is it possibly due to using an int and its rounding?

    Any help would be appreciated. Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 % 5 = ( 1 / 5 = 0 R 1 )


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    Ok, I think I might be following. Since 5 cannot go into 1 any number of times (0 times), the remainder is equal to the dividend? I'm arriving at this logic based on the answers I received in my previous example:

    index_2[0] = 0
    index_2[1] = 1
    index_2[2] = 2
    index_2[3] = 3
    index_2[4] = 4
    index_2[5] = 5
    index_2[6] = 6
    index_2[7] = 7

    If I re-write it using what I believe to be the correct understanding, then I arrive at:

    0 % 8 = 0
    1 % 8 = 1 (8 cannot go into 1 any number of times, therefore the remainder is equal to the dividend)
    2 % 8 = 2 (8 cannot go into 1 any number of times, therefore the remainder is equal to the dividend)
    etc......

    Is this way of thinking of the issue correct? Thank you for the reply

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yep. So 17 % 8 = 1.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by matrixx333 View Post
    For example, 4 % 8 doesn't have a remainder because the result of the division is 0.5. Likewise for 1 % 8, the result of the division is 0.125. How is it arriving at these numbers? Is it possibly due to using an int and its rounding?
    If everything is being done with ints in C, the compiler will not quietly introduce a floating point calculation without you knowing.

    The result of the % is an int. 1 % 8 is 1. The % operator does not work for non-integral types.

    Mathematically a % b is equivalent to a - (a/b)*b. You are assuming there is some magically real (or floating point - computers don't work with true real values) variable in play. There is not.

    The a/b is integer division which (assuming a and b are positive) means rounding towards zero. So 1/4 is 0, 2/4 is 0, 3/4 is zero, 4/4 is 1, 5/4 is 1, etc. According 1%4 is 1 - 0*4 = 1, 2%4 is 2-0*4 = 2 , 3%4 is 3 - 0*4 = 3, 4%4 is 4 - 1*4 = 0, 5%4 is 5 - 1*4 = 1, .....

    For negative values, the logic is a little different (the rule for integer division with negative values varies with version of the C standard) but I won't bore you with that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    The 17 % 8 = 1 example is easier for me to understand because 8 actually goes into 17 two times and leaves a remainder of 1. That's how I was able to understand the index_1 case I listed. 3 % 2 = 1 because 2 goes into 3 one time with a remainder of 1.

    I was having a hard time with the fractional examples because I didn't understand how the program was arriving at the remainder. Thank you both for your help.

    You provided the exact answer I was looking for grumpy, thanks again.
    Last edited by matrixx333; 10-01-2009 at 06:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. modulus operator with double operands?
    By chickenandfries in forum C Programming
    Replies: 7
    Last Post: 03-28-2008, 07:21 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM