+ Reply to Thread
Results 1 to 18 of 18

MOD with negative numbers

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    MOD with negative numbers

    Hello everybody

    I tried this formula
    Please Login or Register  to view this content.
    The result was 2 not -1

    How did it be done?

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,699

    Re: MOD with negative numbers

    This type of setup would give you -1 but still give you expected results for positive numbers

    =SIGN(-4)*MOD(ABS(-4),3)

    ....or better using zbor's advice....

    =MOD(-4,3*SIGN(-4))
    Last edited by daddylonglegs; 12-31-2014 at 06:37 AM.
    Audere est facere

  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,037

    Re: MOD with negative numbers

    The sign is the same as divisor (+).
    Result is remainder of -4/3 but in other direction (towards next multiplier of 3).

    To get negative divisor should be negative
    =MOD(-4,-3) will return -1.
    Never use Merged Cells in Excel

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: MOD with negative numbers

    I dpn't want the result to be -1 .. I just want to understand how the process done in maths?

  5. #5
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: MOD with negative numbers

    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: MOD with negative numbers

    Excel's Mod function:
    MOD(n,d)=n-d*INT(n/d)

    Which here evaluates to (remembering that INT always rounds down) :
    -4-3*-2
    which equals - 4--6 =2
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: MOD with negative numbers

    Thanks a lot Mr. romperstomper
    you are very helpful

  8. #8
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: MOD with negative numbers

    Quote Originally Posted by romperstomper View Post
    Excel's Mod function:
    MOD(n,d)=n-d*INT(n/d)

    Which here evaluates to (remembering that INT always rounds down) :
    -4-3*-2
    which equals - 4--6 =2
    But -4/3= -1.333333333
    So INT this number should be -1 !!!

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: MOD with negative numbers

    Now the twist.
    As noted, the worksheet function MOD(-4,3) returns 2
    But, in VBA,
    Please Login or Register  to view this content.
    Given any integer X and a positive B, if one want the positive version of X modulo B, one must use an expression like

    Please Login or Register  to view this content.
    Another difference is that the worksheet function MOD will return non-integer values
    =MOD(4.6, 3) returns 1.6, while VBA rounds the result to an integer.
    Please Login or Register  to view this content.
    Last edited by mikerickson; 12-31-2014 at 10:40 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: MOD with negative numbers

    Quote Originally Posted by YasserKhalil View Post
    But -4/3= -1.333333333
    So INT this number should be -1 !!!
    Nope - as I said:
    remembering that INT always rounds down
    For negative numbers, rounding down is not the same as rounding towards 0, so -4/3 rounds to -2 not -1.

  11. #11
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: MOD with negative numbers

    Quote Originally Posted by YasserKhalil View Post
    But -4/3= -1.333333333
    So INT this number should be -1 !!!
    Mathematically, no. INT is the "largest integer less than or equal to" its argument. Note that -1 is greater than -1.33333333333333. For example, =(-1 > -1.33333333333333) returns TRUE.

  12. #12
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,037

    Re: MOD with negative numbers

    Less than -1.333 is -2

    Edit: Oh, yes, sorry. You point that out.

  13. #13
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: MOD with negative numbers

    thanks for these valuable information.. you are awesome guys!

  14. #14
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: MOD with negative numbers

    I'm confused now with mod as a function in a worksheet and mod in vba
    I tested
    Please Login or Register  to view this content.
    and
    Please Login or Register  to view this content.
    and the results are different why?

    Another point the result of the following formula results in -2 (the result is negative)
    Please Login or Register  to view this content.

  15. #15
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: MOD with negative numbers

    Quote Originally Posted by YasserKhalil View Post
    I'm confused now with mod as a function in a worksheet and mod in vba ... the results are different why?
    In a sense, the results are the same. in that (-2 mod 3) = (1 mod 3).
    By that I mean that (restricted to integers) X mod 3 has only 3 possible results. Each of these results is equivalent to either 0, 1 or 2.
    (Yes, I know that by this "definition" X mod N = X is true). For doing arithmetic, they are both consistent.

    Another point the result of the following formula results in -2 (the result is negative)]=mod(4,-3)
    This is true, since 4 = (-2*-3) + (-2). It is also the result from romper stomper's formula.

  16. #16
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: MOD with negative numbers

    Do you mean that :
    n-d*INT(n/d)
    is equivalent to the the function MOD(n,d)?

  17. #17
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: MOD with negative numbers

    Quote Originally Posted by YasserKhalil View Post
    I'm confused now with mod as a function in a worksheet and mod in vba[.] I tested
    =Mod(-4,3)
    and
    Msgbox -4 Mod 3
    and the results are different why?

    Another point the result of the following formula results in -2 (the result is negative)
    =mod(4,-3)
    Because in Excel, =MOD(a,b) is calculated by a - b*INT(a/b).

    But in VBA, a Mod b is calculated by a - b*(a\b).

    Note the backslash division operator, not the forward-slash division operator.

    In VBA, the backslash division operator returns the integer part of the division.

    In contrast, in both VBA and Excel, forward-slash division returns the real-number result (e.g. 1 + 1/3 for 4/3), and INT(a/b) returns the "largest integer less than or equal to" the parameter.

    That is not the same as the "integer part" of the division. In effect, the "integer part" is rounded toward zero, whereas the "largest integer less than or equal to" is rounded to the left (toward zero for positive values; away from zero for negative values).

    PS.... When comparing expressions like MOD(-4,3) and -4 Mod 3, it is important to explicitly parenthesize the latter like (-4) Mod 3 to ensure that you get equivalent interpretations. In this case, that is exactly the way that VBA interprets the unparenthesize expression. But for example, compare =-4^2 in Excel and -4^2 VBA.

    [EDIT] Excel interprets that as (-4)^2, whereas VBA interprets it as -(4^2).
    Last edited by joeu2004; 12-31-2014 at 09:31 PM.

  18. #18
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: MOD with negative numbers

    PS....
    Quote Originally Posted by joeu2004 View Post
    Because in Excel, =MOD(a,b) is calculated by a - b*INT(a/b).
    But in VBA, a Mod b is calculated by a - b*(a\b).
    Note that in VBA, a - b*INT(a/b) returns the same thing as MOD(a,b) in Excel,
    which again is different from a - b*(a\b).

    [EDIT] Beating a dead horse ;-) .... Also note that in Excel, a - b*TRUNC(a/b) returns the same thing as a Mod b in VBA because Excel TRUNC rounds toward zero just like the VBA backlash division operator.
    Last edited by joeu2004; 12-31-2014 at 09:55 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] SUMIF positive and negative numbers - take double a negative number?
    By Zordrail in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-16-2014, 08:34 AM
  2. negative numbers w/o negative sign
    By whtbullitt in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-31-2009, 05:07 AM
  3. Replies: 5
    Last Post: 05-12-2009, 04:47 PM
  4. Replies: 1
    Last Post: 11-20-2008, 01:52 AM
  5. Replies: 0
    Last Post: 08-23-2005, 03:59 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1