Re: math:mod

new topic     » goto parent     » topic index » view thread      » older message » newer message
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 thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu