Thread: 1 modulo 2

  1. #1
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42

    1 modulo 2

    How does the result of 1 % 2 = 1? I get that with 3 % 2, two goes in once and one is left over. But two, however, doesn't go into 1 once it goes in 0. Is the reminder the 1 it didn't divide into?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    1/2=0.5 rounded up to 1
    3/2=1.5 drop the 1 and .5 gets rounded up to 1

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by userxbw View Post
    1/2=0.5 rounded up to 1
    3/2=1.5 drop the 1 and .5 gets rounded up to 1
    Not really the correct answer.

    Edit: Please show how 1%3 and 4%3 are 1 using your logic.

    Tim S.
    Last edited by stahta01; 12-03-2017 at 08:20 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    point whatever is the marking of the remainder of division. 5 and up gets rounded up to the next highest number, that is basic math. but too the 5 can be argued to get rounded down too, that is left up to whomever, and you know as well as I do thier is no real editing of ones post after how long?
    Code:
    int main()
    {
        printf("1 % 2  = %d\n", 1%2);
        printf("3 % 2 = %d\n", 3%2);
        printf("4  % 3 = %d\n", 4%3);
        printf("1 % 3 = %d\n", 1%3);
    return 0;
    }
    Code:
    $ ./term2
    1 % 2  = 1
    3 % 2 = 1
    4  % 3 = 1
    1 % 3 = 1
    would evaluate to 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1, while "9 mod 3" would evaluate to 0 because the division of 9 by 3 has a quotient of 3 and leaves a remainder of 0;
    same basic logic if it has a remainder it is round up to 1 or as they put it elevated to 1.
    Last edited by userxbw; 12-03-2017 at 08:47 PM.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by userxbw View Post
    1/2=0.5 rounded up to 1
    3/2=1.5 drop the 1 and .5 gets rounded up to 1
    That's not the correct logic to follow. It may work for 1%2, but what about 5%3 or 15%6. Using the way you rationalize it, 5%3 would be 1 because 5/3 is 1.666, drop the one, 0.666 gets rounded up to 1... But 5%3 is 2, not 1.

    There is a way to make it work though, if you're interested. The remainder is formally described as (dividend - quotient*divisor). In programming terms( not C or C++ ):
    Code:
    rem = a - floor(a/b)*b
    Devoted my life to programming...

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by GReaper View Post
    That's not the correct logic to follow. It may work for 1%2, but what about 5%3 or 15%6. Using the way you rationalize it, 5%3 would be 1 because 5/3 is 1.666, drop the one, 0.666 gets rounded up to 1... But 5%3 is 2, not 1.

    There is a way to make it work though, if you're interested. The remainder is formally described as (dividend - quotient*divisor). In programming terms( not C or C++ ):
    Code:
    rem = a - floor(a/b)*b
    well the logic they use it looks to me that because it is above 5, because it is 6 so it gets elevated to 2, so on and so forth. Looks to me that this is just a made up system to deal with fractions. so their has to be some kind of logic behind it to eliminate the actual fractions, making them whole numbers instead.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by userxbw View Post
    well the logic they use it looks to me that because it is above 5, because it is 6 so it gets elevated to 2, so on and so forth. Looks to me that this is just a made up system to deal with fractions. so their has to be some kind of logic behind it to eliminate the actual fractions, making them whole numbers instead.
    I mean you're very correct that you won't have to do fractions with modulus, but I think it can be clearer than that.

    It would probably help to mod by the same divisor so we start to see a pattern, and it feels less made up...

    1 mod 5 = 1
    6 mod 5 = 1
    7 mod 5 = 2
    8 mod 5 = 3
    10 mod 5 = 0

    Dividends less than divisor will result in the dividend as the result. Multiples of the divisor will be 0. After that, you basically can just divide and the result will be a remainder. A place where most people see modular arithmetic in the everyday is the 12 hour clock, because we go past the twelfth hour once every day and start over, so that might help it seem realistic as well.

    Yet another way to look at it that should work in all situations, like with negative numbers: C99 demands that a/b * b + a%b must result in a. It makes sense logically, right? I mean as long as we remember that we're doing integer division all of our examples work.

    1/5 * 5 + 1%5 = 0 * 5 + 1%5 = 0 + 1 = 1
    6/5 * 5 + 6%5 = 1 * 5 + 6%5 = 5 + 1 = 6
    7/5 * 5 + 7%5 = 1 * 5 + 7%5 = 5 + 2 = 7
    8/5 * 5 + 8%5 = 1 * 5 + 8%5 = 5 + 3 = 8
    10/5 * 5 + 10%5 = 2 * 5 + 10%5 = 10 + 0 = 10

    Substitute numbers as you please.
    Last edited by whiteflags; 12-04-2017 at 06:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with modulo
    By Dave Couture in forum C Programming
    Replies: 5
    Last Post: 08-19-2012, 08:32 AM
  2. How to modulo to this digit
    By .C-Man. in forum C Programming
    Replies: 12
    Last Post: 02-17-2011, 08:27 AM
  3. Modulo 12
    By jack_carver in forum C Programming
    Replies: 12
    Last Post: 04-10-2010, 04:11 AM
  4. Modulo
    By Lurker in forum C Programming
    Replies: 3
    Last Post: 03-24-2003, 09:17 AM
  5. Modulus/Modulo?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2002, 11:29 PM

Tags for this Thread