How to use DIV and MOD

Alex_ChowAlex_Chow Member Posts: 5,063
edited 2011-06-14 in NAV Tips & Tricks
Sorry guys, I keep having to search to get the results. For some reason DIV and MOD usage are not explained anywhere on the online help. So I just want to put this here so I can book mark to it. And yes, I do use mibuso for some of my code repository.

x := 5 DIV 2
y := 5 MOD 2

RESULT:
x = 2
y = 1

Comments

  • DenSterDenSter Member Posts: 8,304
    DIV is integer division. 5 DIV 2 equals 2 because 2 times 2 equals 5 or less.

    MOD is Modulus (I think, not quite sure about the word) but gives you the remainder of integer division.

    So ((5 DIV 2) * 2) + (5 MOD 2) = 5.

    I always have to put it on a test form to figure out which one is which :oops:
  • garakgarak Member Posts: 3,263
    :-k i doesn't understand the problem (if there is one)
    Do you make it right, it works too!
  • kinekine Member Posts: 12,562
    garak wrote:
    :-k i doesn't understand the problem (if there is one)

    No, there is no problem, just lack of documentation. But these functions is common in other languages and it is not problem to find the meaning...

    http://en.wikipedia.org/wiki/Modulo_operation

    I think that DIV do not need more explanation... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • garakgarak Member Posts: 3,263
    edited 2008-09-19
    ah, ok ... (never checked if modulo or division is in the online help..)

    is this known :?:
    for i := 10 DownTo 1 do begin
    end;
    
    Do you make it right, it works too!
  • David_SingletonDavid_Singleton Member Posts: 5,479
    My NAV reference guide, is actually a Pascal developers guide.

    Great book, an it has all this stuff in it.
    David Singleton
  • Alex_ChowAlex_Chow Member Posts: 5,063
    Well.. I just put it on here because I get confused which is which as well. So now we all know in a thread with a clear topic.

    And knowing is half the battle. :mrgreen:
  • DenSterDenSter Member Posts: 8,304
    You should post these in the book wiki, that's a really good place for reference material like this.
  • ShonatinaShonatina Member Posts: 114
    HI ALEX :)

    The Integer Divide (DIV) OperatorThe integer divide operator is used only as a binary operator. Its purpose is to
    divide the numeric term preceding it by the numeric term following it. The result
    type of this division is always of type Integer. If the second term is zero (0), a
    run-time error occurs. Any decimals that resulted from an ordinary division are
    dropped. Therefore, the result of 17 DIV 8 is 2, whereas the result of 17 DIV 9 is
    1.
    The Modulus (MOD) OperatorThe modulus operator (or the remainder operator) is used only as a binary
    operator. Its purpose is to divide the numeric term preceding it by the numeric
    term following it by using the integer division method and then return the
    remainder of that division. The result of this operation is always of type Integer.
    If the second term is zero (0), a run-time error occurs. The following shows
    examples of modulus operator usage.
    • 17 MOD 8 = 1
    • 17 MOD 9 = 8
    The modulus operator requires two numbers. The first number is the one that is
    converted by using the modulus function and the second number represents the
    number system being used. By definition the number system starts at zero and
    ends at the second number minus one. For example, if the second number is ten,
    the number system that is used is from zero to nine. Therefore, the modulus
    represents what the first number converts to, if the numbering system only had
    the number of values indicated by the second number and the first number is
    forced to restart from zero.
    The following example shows several modulus operations:
    • 15 modulus 10 is 5 (because 9 is the last number available, 10 is
    represented by going back from the start, or zero, 11 is 1, 12 is 2, and
    so on)
    • 6 modulus 10 is 6
    • 10 modulus 10 is 0
    • 127 modulus 10 is 7
    The result is the same if the first number is divided by the second by using an
    integer only and the remainder is returned as the value.
    smile always
    shona
    That which you seek inside is that which you find outside
  • SavatageSavatage Member Posts: 7,142
    I use it in a check when we split PO's due to 1/2 30 days 1/2/ 60 days terms.
    We used to manually cut the Qty to Invoice in half , which therefor cuts the invoice amount in half.
    Then we post with 30 days terms. Then we post the (remaining) other half with 60 days terms.
    Giving us two vendor ledger entries with the correct terms. But then we started getting these terms on PO's that had 100 items and manually this was exhausting. So using MOD I can see if the amount is cleanly divisible by 2 & if not it gives us a message to check the Item and handle that manually.

    Hail MOD!!
    Remainder := Purchline."Quantity Received" MOD 2;
    IF Remainder <> 0 THEN MESSAGE('Item %1 Needs Rounding',Purchline."No.");
    OnPush()
    IF NOT DIALOG.CONFIRM(Text1,TRUE)
     THEN EXIT
     ELSE
      Purchline.RESET;
      Purchline.SETRANGE(Purchline."Document Type","Document Type");
      Purchline.SETRANGE(Purchline."Document No.","No.");
      IF Purchline.FIND('-') THEN
        REPEAT
         IF Purchline."Quantity Received" <>0 THEN BEGIN
          Purchline."Qty. to Invoice" := (Purchline."Quantity Received" / 2);
          Remainder := Purchline."Quantity Received" MOD 2;
          IF Remainder <> 0 THEN MESSAGE('Item %1 Needs Rounding',Purchline."No.");
          Purchline.VALIDATE("Qty. to Invoice");
          Purchline.MODIFY;
         END;
        UNTIL Purchline.NEXT = 0;
    
  • Gijs_FranssensGijs_Franssens Member Posts: 10
    Hello,

    Has anyone tried to use DIV in combination with BigInteger in NAV2009 RTC?
    If you execute following code in NAV2009 you'll get following messages:
    LText := FORMAT(DELCHR(PBankAccountNo,'=',DELCHR(PBankAccountNo,'=','0123456789')) + 'BE' + '00');
      FOR i := 1 TO STRLEN(LText) DO BEGIN
        LConvertText := LConvertText + FORMAT(GetRomanNoIBAN(COPYSTR(LText, i, 1)));
      END;
      EVALUATE(LAccountNo, LConvertText);
      LRest := LAccountNo - ((LAccountNo DIV 97) * 97);
      MESSAGE(format(LREST));
    

    Message in Classic Client (which is correct):
    Microsoft Dynamics NAV Classic
    88
    OK

    Message in Role Tailored Client (which is incorrect):
    Microsoft Dynamics NAV

    235002258041864280
    OK

    Anyone got any ideas?
  • usabrangerusabranger Member Posts: 2
    can you explain div with negative numbers. For example: -2 mod 4, or -3 mod 4.
  • vaprogvaprog Member Posts: 1,116
    The result of DIV tells you, how many times the divisor fits within the dividend and is the same as the full decimal expansion of the result with all decimals cut off.

    The reminder is dividend minus quotient times dividend.

    so, if
    A DIV B = Q Then
    A MOD B = A - B * Q

    -2 DIV 4 = 0
    -2 MOD 4 = -2 - 0 * 4 = -2

    -3 DIV 4 = 0
    -3 MOD 4 = -3 - 0 * 4 = -3

    -5 DIV 4 = -1
    -5 MOD 4 = -5 - (-1) * 4 = -5 + 4 = -1
Sign In or Register to comment.