Thread: Integer Division vs Modulo Operation

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    6

    Question Integer Division vs Modulo Operation

    In integer division&nbsp; 6 / 5 = 1.2&nbsp; with just the 1 being returned.&nbsp;<br><br>In modulo 6 % 5 = 1<br><br>On a calculator&nbsp; 6 % 5&nbsp; =&nbsp; 1.2&nbsp; &nbsp;<br><br>I was expecting the 1.2&nbsp; to be a 1.1&nbsp;&nbsp;<br><br>Am I missing something ?&nbsp;
    Last edited by PaulGureghian1; 5 Days Ago at 05:25 PM. Reason: I wanted it to look better.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    471
    Yep... In integer division 6/5 = 1 and you get 1 as remainder (not "modulo")...
    6%5 gives you the remainder... here's a test... -6 mod 5 = 4 (positive) because the range is 5. As in:

    0,1,2,3,4 - counting backwards 6 times from 0 to 4, 3, 2, 1... you'll get 4...

    Code:
    $ cc -x c -include stdio.h - <<< 'int main(void){printf("%d\n",-6%5);}'
    $ ./a.out
    -1
    As you see, you get the remainder, not modulo.

    Don't trust me... click here.
    Last edited by flp1969; 5 Days Ago at 06:24 PM.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    471
    PS: Python operator % will give you the modulo:

    Code:
    $ python -c 'print -6%5'
    4

  4. #4
    Registered User
    Join Date
    Sep 2019
    Posts
    6
    Thanks for the reply. as you saw, my post was kinda unreadable. what tags do I add and how do I add them to make it more readable ?

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    To get you started...

    &lt;&lt; !! Posting Code? Read this First !! &gt;&gt;

    Some other tags are "quote" "url" and are used like this...

    [Tag] ... [/Tag]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Integer division by zero
    By Red Man in forum C Programming
    Replies: 2
    Last Post: 04-10-2016, 12:30 PM
  2. Parsing an Integer with modulus and division
    By HoldenR in forum C Programming
    Replies: 6
    Last Post: 10-24-2012, 12:24 PM
  3. modulo(%) operation with double
    By jpnavarini in forum C Programming
    Replies: 5
    Last Post: 06-20-2009, 10:58 AM
  4. Replies: 7
    Last Post: 06-01-2008, 07:47 AM
  5. modulo 2 division question help
    By shaq8u in forum C Programming
    Replies: 9
    Last Post: 08-20-2003, 08:37 AM

Tags for this Thread