Thread: Modulus Division?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Modulus Division?

    Good evening!

    I am doing some homework. As part of the solution, I want to implement an algorithm that implements modulus division. I was wondering if C had an operator which does just that?
    I know of the % operator, which gives you the remainder of two numbers ( 27 % 7 = 6) and that works fine 99% of the time. The only part where it fails is when I get into negative numbers. If I do - 3 % 9 in C, the answer returns -3, which makes sense as the remainder in -2. However in modulus division the answer would be 6 (modulus division considers - 3 mod 9 as -1 + (6/9), hence the answer is 6). Is there an operator that would do this sort of operation?

    I know in either VB or C# you had the "mod" reserved word which would do that nicely. A similar one exist in C?

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    You could probably write your own wrapper function to detect negative parameters, and then do "the right thing".

    One more thing, % on negative numbers has implementation-specific behaviour in C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Shamshir View Post
    If I do - 3 % 9 in C, the answer returns -3, which makes sense as the remainder in -2. However in modulus division the answer would be 6 (modulus division considers - 3 mod 9 as -1 + (6/9), hence the answer is 6).
    What? Why on earth would you expect to get -2 for the remainder?
    Quote Originally Posted by Shamshir View Post
    Is there an operator that would do this sort of operation?
    You mean an operator that would give you the completely wrong answer? Why would you want that?


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

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by quzah View Post
    What? Why on earth would you expect to get -2 for the remainder?You mean an operator that would give you the completely wrong answer? Why would you want that?


    Quzah.
    Woah relax, it was just a typo. So -3 is the remainder.

    Quote Originally Posted by Salem View Post
    You could probably write your own wrapper function to detect negative parameters, and then do "the right thing".

    One more thing, % on negative numbers has implementation-specific behaviour in C.
    Yeah, I was thinking along those lines, just was wondering if a reserved word/operator existed which would save me the trouble. But alas, here's to more lines of code!

    Thanks for your help.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I don't agree with this "implementation-specific" approach to modulus.
    divident = divisor*quotent + remainer
    That formula implies that if divident or divisor ( or both ) are negative, the remainer will surely be negative and quotent will be negative if the divident XOR divisor are negative, otherwise it'll be positive.
    Devoted my life to programming...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Modulo operation - Wikipedia, the free encyclopedia
    According to this, C89/C90 is implementation-defined and C99 is defined to use the Dividend for the sign of the result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BigInt division and modulus
    By User Name: in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2010, 03:54 PM
  2. Modulus to Bitwise
    By blurrymadness in forum C Programming
    Replies: 1
    Last Post: 12-01-2009, 12:49 PM
  3. Modulus division
    By WatchTower in forum C Programming
    Replies: 4
    Last Post: 07-20-2009, 11:26 PM
  4. Modular Division problem
    By Malek in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2003, 06:08 PM
  5. using modulus & weighting factors
    By task in forum C Programming
    Replies: 4
    Last Post: 09-11-2002, 05:52 PM