Results 1 to 5 of 5

Thread: Divisor modulo function

  1. #1
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default Divisor modulo function

    Modulo (divisor) function as found in a lot of programming languages: Python (%), Ruby (%), Matlab (mod), Fortran (modulo), Common Lisp (mod) and so on.. This is not the same as the remainder operator found in Pascal (mod), C (%), Java (%) and PHP (%).. and so on. Tho note that positive numbers will be the same, so: (Modulo(10,7) = 10 mod 7) but (Modulo(-10,7) <> -10 mod 7)... or (Modulo(10,-7) <> 10 mod -7).. The first one returns -4, and the second one returns 3.
    You will notice that a the mod operator in pascal retains the sign of X, while modulus (function i posted) retains the sign of Y. Zero (0) on the right side will result in Division by zero exception.

    pascal Code:
    function Modulo(X, Y: Double): Double; overload;
    begin
      Result := X - Floor(X / Y) * Y;
    end;

    function Modulo(X, Y: Double): Integer; overload;
    begin
      Result := X - Floor(X / Y) * Y;
    end;

    A property of this is that it can generally replace functions like FixD, FixRad (and similar).

    EG:
    pascal Code:
    begin
      WriteLn(Modulo(364.5, 360));     //FixDeg (always in range of 0 to 360)
      WriteLn(Modulo(-60, 360));

      WriteLn(Modulo(1.0,-360));       //Always in range of 0 to -360.
      WriteLn(Modulo(-361.0,-360));    
     
      WriteLn(Modulo(6.5340, PI+PI));  //FixRad
      WriteLn(Modulo(-1.1977, PI+PI));
    end.
    Last edited by slacky; 08-01-2017 at 12:41 PM.
    !No priv. messages please

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Are you absolutely sure that your formula is correct? I mean.. I don't mean any harm but it prints 2.0f for the remainder of -10.0f / 3.0f. Pretty sure the value should be -1.0f as the remainder.




    Yours prints 2.0f :S

    Yes I understand that there are three ways to do it.. With floor, euclidean and truncated division but then for simple arithmetic, it will yield unexpected results.. I do not understand why `-10 % 3` would be `2`. Why round downwards when it's already negative.

    Yes Google's calculator also does that and so does Wolfram's but is 2 really the right value. 2 isn't the remainder of 10 % 3 so why should it be the remainder of -10 % 3? The remainder really should be -1.



    So again, why use Floor instead of Trunc? In fact, C uses truncated division in fmod.

    I'd personally prefer:

    Simba Code:
    Function Modulo(X, Y: Extended): Extended; overload;
    Begin
      Result := X - Trunc(X / Y) * Y;
    End;
    Last edited by Brandon; 02-07-2014 at 07:54 PM.
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    Are you absolutely sure that your formula is correct? I mean.. I don't mean any harm but it prints 2.0f for the remainder of -10.0f / 3.0f. Pretty sure the value should be -1.0f as the remainder.
    ...
    .. There is a difference between modulus, and remainder.
    The % operator, and fmod in C is not the canonical modulus operator, both gives the remainder. At google, wolfram alpha (+ buckload of programming languages) the % or mod refers to the modulus operation (returns the modulus after division), rather then the remainder.

    There is plenty enough to read on the subject on the web. Some quick searches at Google should yield plenty of reading material.
    For example http://www.mathworks.se/help/matlab/ref/mod.html
    Last edited by slacky; 08-01-2017 at 12:42 PM. Reason: spelling
    !No priv. messages please

  4. #4
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by warpie View Post
    .. There is a difference between modulus, and remainder.
    The % operator, and fmod in C is not the canonical modulus operator, both gives the remainder. At google, wolfram alpha (+ buckload of programming languages) the % or mod refers to the modulus operation (returns the modulus after division).
    -10 % 3 = 2 because -10 + 3*4 = 2

    There is plenty enough to read on the subject on the web. Some quick searches at Google should yield plenty of reading material.
    EG: http://www.mathworks.se/help/matlab/ref/mod.html

    Yeah, I read these:

    http://mathworld.wolfram.com/Congruence.html
    http://blogs.msdn.com/b/ericlippert/...s-modulus.aspx
    http://legacy.cs.uu.nl/daan/download...divmodnote.pdf


    But the only uses I see is really for time and angles.. Who knew that Trunc vs. Floor could cause so much of a difference. I don't think I'll ever get used to seeing modulo returning "non-remainder" values outside of a math class..


    It is this statement that makes what you have confusing:

    Also, this works with floating point values, unlike "mod"-operator, which just might come in handy from time to time.
    See your implementation isn't equivalent to "mod" for floating point numbers..
    Last edited by Brandon; 02-08-2014 at 12:55 AM.
    I am Ggzz..
    Hackintosher

  5. #5
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    It is this statement that makes what you have confusing...
    I have removed that sentence, as you state it is very misleading.

    Yeah, the usage might be soemwhat small, well as for positive numbers it can be used for whatever (pascal) mod is used for, tho the built in op should be the first choice. It can also be used for indexing arrays like structures, allowing a "wrap around"-feature. indexing -1 to get the last item in the array.
    Last edited by slacky; 01-11-2015 at 02:41 AM.
    !No priv. messages please

Thread Information

Users Browsing this Thread

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

Posting Permissions

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