SRL Archive - General

Divisor modulo function


slacky

2014-02-07 12:46:21

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.


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:

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.

Brandon

2014-02-07 13:50:59

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:


Function Modulo(X, Y: Extended): Extended; overload;
Begin
Result := X - Trunc(X / Y) * Y;
End;

slacky

2014-02-07 19:01:51

From: Brandon
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

Brandon

2014-02-07 19:51:16

From: warpie
.. 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/archive/2011/12/05/what-s-the-difference-remainder-vs-modulus.aspx
http://legacy.cs.uu.nl/daan/download/papers/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..

slacky

2014-02-07 20:51:10

From: Brandon
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.