1. math:mod

mod:namespace math

public function mod (object x, object y)

Compute the remainder of the division of two objects using
floored division.

Parameters:

dividend : any Euphoria object.
divisor : any Euphoria object.

Returns:

An object, the shape of which depends on dividends and divisors. For two atoms, this is the remainder of dividing dividend by divisor, with divisor's sign.

Comments:

There is a integer N such that dividend = N * divisor + result.

    • 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)      =  -4 
Note: if you are using floored division: -9 mod -5 = 1 Also mod is taking the sign of divisor not the dividend.

new topic     » topic index » view message » categorize

2. Re: math:mod

bill said...

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.

new topic     » goto parent     » topic index » view message » categorize

3. Re: math:mod

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.

new topic     » goto parent     » topic index » view message » categorize

4. Re: math:mod

bill said...

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.

bill said...

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

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu