modulo

Claude Pache claude.pache at gmail.com
Mon Sep 16 06:28:03 PDT 2013


Le 16 sept. 2013 à 13:40, Claude Pache <claude.pache at gmail.com> a écrit :

> 
> Le 16 sept. 2013 à 11:30, raul mihaila <raul.mihaila at gmail.com> a écrit :
> 
>> Hello,
>> 
>> I think the definition of modulo is wrong. http://people.mozilla.org/~jorendorff/es6-draft.html#sec-5.2 
>> k should be of the same sign as x not as y.
> 
> I think the definition is correct, and is more useful than the opposite convention. It is indeed the opposite convention of the one used by the remainder operator (`%` in JS), but it is not the remainder operator. (BTW, is there any chance that the modulo operator [1] will be introduced in EcmaScript, rather sooner than later?)
> 
> [1] http://wiki.ecmascript.org/doku.php?id=strawman:modulo_operator
> 
> In fact, since `x modulo y` is apparently only use with positive (and integral) `y` in the specification, the definition should be simplified, by restricting to the case where `y` is positive.

I have almost opened a duplicate of [bug 980]. I find that the somewhat convoluted definition given at the end of section 5.2:
	
  The notation “x modulo y” (y must be finite and nonzero) computes a value k of the same sign as y (or zero) such that abs(k) < abs(y) and x−k = q × y for some integer q.

can be usefully simplified and made clearer by saying:

  The expression “x modulo y”, where y is a positive integer, evaluates to the unique value k such that  0 ≤ k < abs(y)  and  x − k = q × y  for some integer q.

To the reason given not to simplify ("it doesn't hurt and maybe someday in the future it will be needed"), there is a simple one-word response: YAGNI.

At least, restricting to positive numbers for the second operand has the following concrete advantage: it saves the reader from useless metaphysical question about the sign of the result. (And for restricting to integers?... uh...  just YAGNI.)

—Claude

[bug 980]  https://bugs.ecmascript.org/show_bug.cgi?id=980


> 
>> 
>> Also, shouldn't ToUint32 and ToUint16 return only positive numbers? If the argument is, for example, -3, the result will be -3 (assuming that the modulo definition is wrong). Or maybe the argument will never be negative?
> 
> Assuming the modulo definition is correct, `ToUint32` and `ToUint16` work as expected (if the argument is `-3` the result is `2^32-3`, resp. `2^16-3`.
> 
> —Claude
> 
> 
>> 
>> Raul M
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130916/82116a11/attachment.html>


More information about the es-discuss mailing list