1. math:mod
- Posted by bill May 12, 2012
- 1385 views
mod:namespace math
public function mod (object x, object y)
Compute the remainder of the division of two objects using
floored division.
Parameters:
divisor : any Euphoria object.
Returns:
Comments:
- The result is non-negative and has lesser magnitude than divisor.** N needs not fit in a Euphoria integer.
The result has the same sign as the dividend. When both arguments have the same sign, mod() and remainder() return the same result.
This differs from remainder() in that when the operands' signs are different this function rounds dividend/divisior away from zero whereas remainder() rounds towards zero.
mod mod(9,5) = 4 mod(9,-5) = -1 mod(-9,5) = 1 mod(-9,-5) = -4Note: if you are using floored division: -9 mod -5 = 1 Also mod is taking the sign of divisor not the dividend.
2. Re: math:mod
- Posted by DerekParnell (admin) May 12, 2012
- 1353 views
The result has the same sign as the dividend.
I think what you are trying to make clear, is that the documentation for this function is wrong, in that the result has the same sign as the divisor and not that of the dividend.
3. Re: math:mod
- Posted by bill May 12, 2012
- 1349 views
Not exactly:
Modulus mathmatically:
- x modulus 0: is undefined.
- x modulus n: is a number in the range 0 .. |n|-1
- where |a| = absolute value of a.
Hence the statement:
- The result is non-negative and has lesser magnitude than divisor.
The mod function uses floored division which would
mean that:
-9 mod 5 = floor(-1.8) = -2 * 5 + 1 => 1 -9 mod -5 = floor(1.8) = 1 * -5 + -4 => -4 9 mod -5 = floor(-1.8) = -2 * -5 + -1 => -1 9 mod 5 = floor(1.8) = 1 * 5 + 4 => 4
As -1 mod 5 equals 4 mod 5 and
- -4 mod 5 equals 1 mod 5
the sign of the dividend is irrelevant.
Clearly the mod function is not modulus.
and secondly the documentation is seriously
misleading.
This needs to be fixed.
4. Re: math:mod
- Posted by mattlewis (admin) May 13, 2012
- 1301 views
Not exactly:
Modulus mathmatically:
- x modulus 0: is undefined.
- x modulus n: is a number in the range 0 .. |n|-1
- where |a| = absolute value of a.
Hence the statement:
- The result is non-negative and has lesser magnitude than divisor. [/quote]
It looks like we have some contradictory documentation. The line right after that states that we're using the sign of the dividend. There's no requirement that the result be non-negative (from a mathematical perspective), unless we were talking about least non-negative residues, which we aren't.
The mod function uses floored division which would
mean that:
-9 mod 5 = floor(-1.8) = -2 * 5 + 1 => 1 -9 mod -5 = floor(1.8) = 1 * -5 + -4 => -4 9 mod -5 = floor(-1.8) = -2 * -5 + -1 => -1 9 mod 5 = floor(1.8) = 1 * 5 + 4 => 4
As -1 mod 5 equals 4 mod 5 and
- -4 mod 5 equals 1 mod 5
the sign of the dividend is irrelevant.
Clearly the mod function is not modulus.
and secondly the documentation is seriously
misleading.
This needs to be fixed.
All we need to do is remove the non-negative, and change dividend to divisor, and everything looks good. Or possibly, we might want to change it to actually use the dividend's sign, as that's how C seems to do it. I suspect that if we checked the history of the code, some optimizations made the function go out of spec by simplifying the calculation to ignore which sign is negative.
Talk about, "Clearly the mod function is not modulus," is a bit hyperbolic and just makes you look foolish.
Matt